|
manual abstract
Several types of loops are available. Each type gives you a different way to exit the loop, based on a conditional test. .Control Toolbar Menu Note:A loop command marks the start of the loop. The correspondingEnd command marks the end of the loop. For...EndFor Loops Note:The ending value can be less than the beginning value, but the increment must be negative. Note:TheFor command automatically increments the counter variable so that the program can exit the loop after a certain number of repetitions. To enter most of the loop-related commands, use the Program Editor’s .Control toolbar menu. :For | :EndFor When you select a loop, the loop command and its corresponding End command are inserted at the If the loop requires arguments, the cursor is cursor location. positioned after the command. You can then begin entering the commands that will be executed in the loop. A For...EndFor loop uses a counter to control the number of times the loop is repeated. The syntax of the For command is: For(variable, begin, end [, increment]) added to the counter each subsequent time For is executed (If this optional value is omitted, the increment is 1.) exits the loop when variable exceeds this value counter value used the first time For is executed variable used as a counter When For is executed, the variable value is compared to the end value. If variable does not exceed end, the loop is executed; otherwise, program control jumps to the command following EndFor. :For i,0,5,1 i > 5 i . 5 : -------: -------: EndFor :------- At the end of the loop (EndFor), program control jumps back to the For command, where variable is incremented and compared to end. Chapter 17: Programming 297 For example: For example: While...EndWhile Loops Note:The While command does not automatically change the condition. You must include commands that allow the program to exit the loop. :For i,0,5,1 Displays 0, 1, 2, 3, 4, and 5. : Disp i :EndFor Displays 6. When variable :Disp i increments to 6, the loop is not executed. A While...EndWhile loop repeats a block of commands as long as a specified condition is true. The syntax of the While command is: While condition When While is executed, the condition is evaluated. If condition is true, the loop is executed; otherwise, program control jumps to the command following EndWhile. :While x<5 x . 5 x < 5 : -------: -------: EndWhile :------- At the end of the loop (EndWhile), program control jumps back to the While command, where condition is re-evaluated. To execute the loop the first time, the condition must initially be true. ¦ Any variables referenced in the condition must be set before the While command. (You can build the values into the program or prompt the user to enter the values.) ¦ The loop must contain commands that change the values in the condition, eventually causing it to be false. Otherwise, the condition is always true and the program cannot exit the loop (called an infinite loop). For example: Initially sets x. :0!x :While x<5 Displays 0, 1, 2, 3, and 4. : Disp x Increments x. : x+1!x :EndWhile Displays 5. When x :Disp x increments to 5, the loop is not executed. 298 Chapter 17: Programming Loop...EndLoop Loops Loop...EndLoop Loops A Loop...EndLoop creates an infinite loop, which is repeated endlessly. The Loop command does not have any arguments. :Loop : -------: -------: EndLoop :------- Typically, the loop contains commands that let the program exit from the loop. Commonly used commands are: If, Exit, Goto, and Lbl (label). For example: :0!x :Loop : Disp x : x+1!x An If command checks : If x>5 the condition. : Exit :EndLoop Exits the loop and jumps to :Disp xhere when x increments to 6. In this example, the If command can be anywhere in the loop. When the If command is: The loop is: At the beginning of the loop Executed only if the condition is true. At the end of the loop Executed at least once and repeated only if the condition is true. Repeating a Loop Immediately Lbl and Goto Loops The If command could also use a Goto command to transfer program control to a specified Lbl (label) command. The Cycle command immediately transfers program control to the next iteration of a loop (before the current iteration is complete). This command works with For...EndFor, While...EndWhile, and Loop...EndLoop. Although the Lbl (label) and Goto commands are not strictly loop commands, they can be used to create an infinite loop. For example: :Lbl START : -------: -------: Goto START :------- As with Loop...EndLoop, the loop should contain commands that let the program exit from the loop. Chapter 17: Programming 299 Configuring the TI-89 / TI-92 Plus configuration of the TI-89 / TI-92 Plus. Because mode changes are particularly useful, the Program Editor’s Mode toolbar menu makes it easy to enter the correct syntax for the setMode command. Configuring the TI-89 / TI-92 Plus configuration of the TI-89 / TI-92 Plus. Because mode changes are particularly useful, the Program Editor’s Mode toolbar menu ma...
Other models in this manual:Calculators - PLUS TI-89 (4.12 mb)