Loops

You already know the WHILE loop. (We discussed it briefly in the section on conditional statements.) Now we'll learn two more useful loops.

A. LOOP command

A LOOP...ENDLOOP block executes everything in the middle indefinitely. IMPORTANT: Remember to include a way to get out of the loop. LOOP...ENDLOOP is equivalent to LBL A...GOTO A. Some say that LOOP suffers from "program fatigue" more than using labels. The TI-89 and TI-92 suffer from fatigue so little that if there is a difference (and I don't think there is), it is too slight to notice. I use LOOP...ENDLOOP myself. Go try some of our earlier examples with the LOOP command.

B. FOR Loop

A FOR loop executes the portion between FOR and ENDFOR a set number of times. The syntax is FOR var,low,high,interval. For x,0,10,2...EndFor would execute the block for x=0, x=2, x=4, x=6, x=8, and x=10 in that order. If the interval is not entered, it is automatically one. The interval can be negative. Remember the meaning of life problem on the last quiz? Let's try it with FOR.

DemoF()
Prgm
For x,1,42
Endfor
Dialog
Title "Ford Prefect says..."
Text "That's the meaning of life!"
Enddlog
Endprgm


Considerably shorter. Of, course something could go inside the FOR loop. Here it acts as a stall.

<<Previous - Contents - Next>>