A loop is a section of code that is executed multiple times. It could be because there are multiple elements that have to receive the same instructions or because the item does have to receive the same instructions multiple times. TI-Basic allows easy ways to execute the same code multiple times.
An indefinite loop is one that does not end. It will repeat continuously until some extraneous situation (like the calculator's batteries being removed) occurs. There are two ways to intentionally create an indefinite loop.
Lbl and Goto have been used previously in this guide, but to prevent a block of code from operating by putting the Lbl command after the Goto command. When the Lbl command is before the Goto command, like when the Lbl command is after, the program goes from the location of the Goto to the location of the Lbl. This can be used to create a loop.
:Lbl 1 :Pause "PRESS ENTER" :Goto 1 :Disp "DONE"
However, the use of Lbl and Goto for loops is discouraged, especially when other ways to create a loop are available.
Normally, programmers will only use labels for menus or very small basic programs.
While is preferred over Lbl and Goto for creating loops, since While can only be used to create loops.
Its format is
While condition(is true)
While will start a loop that will end only when the condition is false.
In Boolean logic true = 1, False = 0 Thus if the condition is 1(true), the condition will remain true and the loop will never end.
The same loop from the previous section can be rewritten:
:While 1 :Pause "PRESS ENTER" :End :Disp "DONE"
Anything after the End is not performed. Anything before the end gets repeated. If there is not an End statement, when the end of the program is reached, there will not be an automatic loop.
Sometimes, we want loops to end.
While, using a condition that can be false, is a way to start an definite loop. Perhaps, from the previous example, we desired to make the user press Enter 10 times, instead of indefinitely. This can be done by changing the While condition and adding a variable to keep track of the number of times enter was pressed.
:0→X :While X<10 :Pause "PRESS ENTER" :X+1→X :End :Disp "DONE"
Any condition can be made a condition for a While statement. However, this type of "Do something a certain number of times" is the most common application. This particular condition can be done using a For( loop.
For is the loop used when something has to be done a certain number of times. It has the form
For(variable, initial, end). This is equivalent, when using the While statement, to:
:initial→variable :While variable<end :variable+1→variable
The For version of the code is much shorter; being able to compress three commands into one. However, this limits the usage of For(, since it can only use compress those three commands.
A duplicate of the While example using the For( command can be seen in action in the below example.
:For(X,0,10 :Pause "PRESS ENTER :End :Disp "Done
The third loop-creating command is the opposite of the While command. The While command repeats a command until the condition is false. This repeats a command until the condition is true. It is in the same format as While:
)condition (is true), it is just While not(.
As an example,here is a program that will tell you all key codes until enter is pressed:
:getKey→K :ClrHome :Repeat K=105 :Repeat K :getKey→K :End :Disp K :End :ClrHome
Examples of loops:
Program:SECRTKEY :Delvar K :0→A :ClrHome :While not(A :Repeat K=105 :Output (1,1,"Press any button :Repeat K :Getkey→K :End :ClrHome :End :A+1→A :End
This program will keep looping until you find the "secret" button (105:Enter)
A more optimised version of this would be...
Program:SECRTKEY :ClrHome :While not(A :Repeat Ans :getKey→K :End :A+(K=105)→A :End
Or another optimised variation of the same program...
Program:SECRTKEY :Delvar KClrHome :Repeat K=105 :getKey→K :end
An example of a simple Homescreen movement program using loops:
:4→A :8→B :ClrHome :While 1 :Output(A,B,"* :Repeat Ans :getKey→K :End :Output(A,B," :A-(K=25 and A>1)+(K=34 and A<8→A :B-(K=24 and B>1)+(K=26 and A<16→B :End