< TI-Basic Z80 Programming < List of Commands

For(
TI Basic programming instruction, accessed from Catalog or programming editor.
Usage:
For(A,B,C[,D])...End
Causes all commands from For( to End to be repeated a certain number of times. A is any letter variable, B is any numerical expression, C is numerical expression, and D is numerical expression.
The For loop first loads the value of B into variable A, and then repeats the code until the End tag, adding the value of D (or 1 if D is not specified) to variable A, until A is greater than C (less than C if D is negative), and then jumps to the End tag without executing the commands.
For example, the code below will display "A" two times.
:For(A,1,2)
:Disp "A"
:End

Explanation:
Start - 1 is loaded into A, Disp "A" is executed, 1 is added to A (A=2), Disp "A" is executed again, 1 is added to A (A=3), A>2, so Disp "A" is not executed again and the program moves on to the next set of commands. Note that you can specify a negative value for D (For(A,2,1,-1)), which would have the same effect if replaced with the For( command in the program above, except that A will start as 2 and end as 0. A can also be used within the code inside the For loop if needed, and it is not recommended to alter A within the For loop for novice programmers, even though it is legal.

Note: in argument For(A,B,C[,D]), if D is negative and C>B, -or- if D is positive or not specified and B>C, the For loop is not executed at all, but the value of B is still stored into A. Also, the increment value (D) cannot be Zero, if D=0 your program will stop with an error. If B=C, your For loop will be executed once. The values of B,C, and D cannot be changed within the For loop (they can be set to different values, but they will not affect the conditions of the loop).

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.