Search code examples
if-statementti-basic

How to limit the size of the block of code in an if-else statment? (TI-BASIC)


I have a problem where code—meant to be executed after an if-else statement—seems to be considered part of the else block.

I have tried using the End operator to no avail.

ClrHome
Input "Dataset List (1-6)-->",L
Input "Value ",X

If L=1 and dim(L₁:Then
L₁→⌊DATA
Else:If L=2 and dim(L₂:Then
L₂→⌊DATA
Else:If L=3:Then
L₃→⌊DATA
Else:If L=4:Then
L₄→⌊DATA
Else:If L=5:Then
L₅→⌊DATA
Else:If L=6:Then
L₆→⌊DATA
Else:Then
Disp "Invalid Input",L,X
End 

Disp ⌊DATA
Pause 

Here is a sample output Output of the function

The last two lines are not executed...


Solution

  • Consider the behavior of this:

    If X:Then
    Disp "X MUST BE TRUE"
    If Y:Then
    Disp "Y MUST BE TRUE"
    End
    Disp "X MUST BE TRUE"
    End
    Disp "I ALWAYS EXECUTE"
    

    What if there was only one End instead?

    If X:Then
    Disp "X MUST BE TRUE"
    If Y:Then
    Disp "Y MUST BE TRUE"
    End
    Disp "I ONLY EXECUTE IF X IS TRUE"
    

    Every If-Then needs an End to go with it, regardless of if there is an Else in the middle. One End at the end of multiple nested If-Then-Elses isn't enough.

    So in order to have the If-Else behavior I assume you want, you'd have to add one End for every If:

    If L=1 and dim(L₁:Then
    L₁→⌊DATA
    Else:If L=2 and dim(L₂:Then
    L₂→⌊DATA
    Else:If L=3:Then
    L₃→⌊DATA
    ...
    End:End:End
    

    However I think you'd be better off with something like this, just for simplicity and readability:

    If L=1:L₁→⌊DATA
    If L=2:L₂→⌊DATA
    ...
    

    Though this has worse performance, which can become noticeable if there are many Ifs.