TI-Basic 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. TI-Basic offers a few types of loops: While, For, and Repeat.

While
The While ( 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.

Examples
The following example demonstrates a very basic <tt>While</tt> Loop that will display  until   equals 0:

For
The <tt>For</tt> (<tt> CTL 4</tt>) loops executes a block of commands between the <tt>For</tt> and <tt>End</tt> commands n number of times. <tt>For</tt> is useful when you know the exact number of times you want to repeat a section of code.

Examples
To print the numbers 1 to 5 using a <tt>For</tt> loop:

This code will print:

To traverse a list and print its elements:

Repeat
The <tt>Repeat</tt> (<tt> CTL 6</tt>) loop executes a block of commands between the <tt>Repeat</tt> and <tt>End</tt> commands until specified condition is true. The condition is tested at the beginning of the loop (when the <tt>End</tt> command is encountered), so if the condition is initially true, the block of commands will never execute.

<tt>Repeat</tt> is simply <tt>While</tt> where the condition is inverted.

Examples
The following example demonstrates the use of a <tt>Repeat</tt> statement. It will continue to ask the user for an  value until  :

You try it!
Try these examples to practice using loops.

Summation from 1 to n
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  ($$1+2+3+4+5+6=21$$).

This example uses the following summation expression: $$\sum_{i=1}^N x_i$$

Fibonacci Sequence
The Fibonacci Sequence is defined as $$F(n) = F(n-1) + F(n-2)$$ where $$F(1) = 0$$ and $$F(2) = 1$$. Therefore, the first 10 elements of the sequence are <tt>0, 1, 1, 2, 3, 5, 8, 13, 21, 34</tt>. Write a program that stores the first 100 elements of the sequence to, then display them. Remember, you will have to instantiate the list first.

FizzBuzz
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:

In this example, we must check multiples of 15 before checking 3 or 5.