Creating loops

Loops are exactly as you would imagine, a circular flow to a program, where it keeps repeating a specific section until the loop is broken. Many games utilize this, since in a loop you will be able to get key presses from the user, which in turn will allow you to make games that have characters that actually move, instead of having the player read text and select options from a menu.


Creating loops are generally not a good idea using this method. The reason being is the Goto's need to start at the top of the program, which can severely slow down your loop. It is also easier to cause a memory leak in a loop like this, if you are not careful.

(A memory leak is when you exit out of a series of code that requires the End command to finish off the flow, or branch of the code. The calculator
records the fact that it should reach an End statement sometime, and will keep track of how many of them it should see. If you do not allow the
calculator to reach the End statements, over time, it will eventually run out of RAM trying to keep track of those statements.

My recommendation is to stay away from using this system as a loop. Below are more efficient ways of creating loops for your games and programs.


The While command will only work while the condition is true. So, say we have While A=1. As long as A is equal to 1, this loop will work. If A is changed or deleted, the loop will no longer work. An interesting usage for While is to replace the whole Goto and Lbl system of moving around in a program, by manipulating the variable value in a While statement. You would need to confine everything in a "main" loop, so you can go back to the begining if you need to. Here is some example code:

While W=1
< Insert code to check for which loop to go to >
While W=2
< code >
While W=4
< code >

This system is very effective, and seems to be faster than the Lbl and Goto commands. There is one downside to this system though. You will have to keep a VERY careful eye on ending each loop correctly, or you will experience some wierd errors, depending on what you are doing. I prefer this method over Lbl and Goto, as there is no lag in the program when running. Please refer back to Lesson 4 for reasons why Goto is slower.


The Repeat command works like the While command but in the opposite way. It creates a loop while the condition is false. Here is a simple program to look at:

Delvar A:Repeat A=100:A+1/->/A:Output(1,1,A:End

That bit of code creates a counter. When the variable reaches 100, the loop is broken. Another difference Repeat has from While is that regardless of whether the loop is true or not, the code withing a Repeat loop will always be executed at least once. The reason behind this is the calculator checks to see if the loop is true or false when it reaches the End command.


This is the last way to create a loop. This loop has a different format, and in a sense is considered a 'limited' loop. Here is the syntax:

A is the variable that is the 'counter' and this is where the For( loop keeps count of where it is at, relative to the ending value.
B is the starting value, C is the ending variable, and D is the 'count by' variable.

To better explain this loop, I shall first explain D. This can be any number, negative or positive, whole number or decimal. If you make this a negative number, you need B to be greater than C. If you go positive, B needs to be less than C. A can then be used to display a number, or can be used to create animation, depending on what you are doing at the time. Also, you do not need D if you are just going to have the loop count up by 1, since that is the default.


This command allows for you to search for key presses inside a game or program. All you do is after the command (inside one of the first 3 loops) store to a variable, like this: getKey/->/K

To define the keys, you can use the following image:
TI-83+ getkey map

A good way to remember the getkey values is to look at the row and column. Count down from and two the right of Y=. How many rows down is the first number(s) and the amount of columns to the right is the last number. Study the above image a bit, and you will see what I mean.

While looking at the getkey map above, you will notice I did not change the ON key. This key is not available to use with getKey as it is meant to interrupt your BASIC program. There are programs that block this key, even make it available for detection, but it is not normally available for you to use as a key in your program.

Command definitions for this lesson:

While - Looping command that will loop through any commands until it reaches its corresponding End statement, while a statement is true.

Repeat - Looping command that will loop until it reaches a corresponding End statement, while statement is false. Will loop at 
least once, regardless if the statement is false or not.

For( - This command creates a loop that has a limited amount of cycles.

getKey - This command allows a programmer to get key input from a user while in a loop.

<<Previous - Contents - Next>>