TIBasic Z80 Programming/Loops
It is important while writing programs to save space due to the calculator's limited memory. To help this, loops can be used which can repeat a section of code a number of times. TIBasic offers a few types of loops: While, For, and Repeat.
WhileEdit
The While (PRGM CTL 5) loop executes a block of commands between the While and End commands as long as the specified condition is true. The condition is tested at the beginning of the loop (when the End command is encountered), so if the condition is initially false, the block of commands will never execute.
SyntaxEdit
While condition is true statements End
 Where condition is any statement resulting in a true/false/zero/nonzero result
 If condition returns true or nonzero, statement continues to execute
 If condition returns false or zero, statement will not execute and will skip to after the End
ExamplesEdit
The following example demonstrates a very basic While Loop that will display X
until Y
equals 0:
 While Y≠0
 Disp X
 End
ForEdit
The For (PRGM CTL 4) loops executes a block of commands between the For and End commands n number of times. For is useful when you know the exact number of times you want to repeat a section of code.
SyntaxEdit
For(variable,begin,end[,increment]) statements End
 Where variable is a Real (AZ & θ). This variable represents a counter.
 Where begin is the number to being counting from
 Where end is the number to stop exectution once variable equals end
 Where, optionally, increment is the amount to change variable after each loop. If this argument is not passed, it is assumed to be 1.
ExamplesEdit
To print the numbers 1 to 5 using a For loop:
 For(I,1,5)
 Disp I
 End
This code will print:
1 2 3 4 5
To traverse a list and print its elements:
 For(I,1,dim(L1))
 Disp L1(I)
 End
RepeatEdit
The Repeat (PRGM CTL 6) loop executes a block of commands between the Repeat and End commands until specified condition is true. The condition is tested at the beginning of the loop (when the End command is encountered), so if the condition is initially true, the block of commands will never execute.
Repeat is simply While where the condition is inverted.
SyntaxEdit
Repeat condition statements End
 Where condition is any statement resulting in a true/false/zero/nonzero result
 If condition returns false or zero, statement continues to execute
 If condition returns true or nonzero, statement will not execute and will skip to after the End
ExamplesEdit
The following example demonstrates the use of a Repeat statement. It will continue to ask the user for an X
value until X > 5
:
 0→X
 Repeat X>5
 Prompt X
 End
You try it!Edit
Try these examples to practice using loops.
Summation from 1 to nEdit
Write a program that asks the user for a number n (must be greater than 1) and prints the sum of the numbers 1 to n. For example, if n = 6, the program would display 21
( ).
Solution  


Fibonacci SequenceEdit
The Fibonacci Sequence is defined as where and . Therefore, the first 10 elements of the sequence are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34. Write a program that stores the first 100 elements of the sequence to L1
, then display them. Remember, you will have to instantiate the list first.
Solution 

:DelVar L1 // List instantiation
:100→dim(L1) // List instantiation
:0→L1(1) // F(1) = 0
:1→L1(2) // F(2) = 1
:For(A,3,100) // We start at 3 since 1 and 2 are already set
:L1(A1)+L1(A2)→L1(A) // F(N) = F(N1) + F(N2)
:End // End for loop
:ClrHome // Clear screen
:For(A,1,100) // From 1 to 100
:Disp L1(A) // Print F(A)
:End // End for loop

FizzBuzzEdit
FizzBuzz is a children's game often used today as a programming evaluation program. The original game consists of players taking turns to count incrementally, replacing any number divisible by 3 with the word "fizz", and any number divisible by 5 with the word "buzz". For this example, write a program that counts from 1 to 100, replacing numbers divisible by 3 with "Fizz", 5 with "Buzz", and 3 and 5 with "FizzBuzz."
Hint: Take the remainder of the current value and the key numbers and compare it to 0.
You will have to get creative writing this program as there are many ways to approach it. The following solution is just one of many solutions:
Solution  

In this example, we must check multiples of 15 before checking 3 or 5.
:ClrHome
:For(X,1,100)
:If remainder(X,15)=0:Then
:Disp "FIZZBUZZ"
:Else
:If remainder(X,3)=0:Then
:Disp "FIZZ"
:Else
:If remainder(X,5)=0:Then
:Disp "BUZZ"
:Else
:Disp X
:End
:End
:End
:End
