Heaney

Soft


HeaneySoft's BASIC Tutorial
Section 5: Looping


There are three statements on the TI-83 made for looping, those are: For, While, and Repeat. The last statement we'll talk about in Section 5 is more of a branching tool, and that's Goto.

For:
The For loop is used when you already know how many times you want the program to loop. For example, in the multiplication program, we know, before the loop starts, exactly how many times the loop will be performed. In order to work, the For loop needs a variable to keep track of the number of times it has looped. The For statement is written as For( variable, start, finish ). Here is an example of the For statement in a program:

:Input "HOW MANY TIMES?",A
:For(B,1,A)
:Disp B
:End

Once again, the End statement is very important. Lets say we ran this program and inputted 3 for A, the output would look like this:

1
2
3

The For loop can be very useful. The next two statements are While and Repeat which I will group as one because they are very similar.

While and Repeat:
Unlike the For statement, While and Repeat evaluate a condition, just like the If statement, and they continue to loop until they get what they want out of the condition. The only difference between the two is that While keeps looping until the statement is false, and Repeat keeps looping until the statement is true, this will make sense when you see the examples. So, to write a program using these statements, all we do is write a conditional statement after them, lets use the A>B statement again. Lets assume A is greater than B, here's an example of While:

:While A>B
:Disp A
:Disp B
:Prompt A
:Prompt B
:End
:Disp "B IS NOW GREATER","THAN OR EQUAL","TO A"

In this While loop the user inputs A and B every time around the loop. If A is greater than B at the end of the loop it starts over again, that's why it's called a "While" loop, because it executes while the statement is true. When the statement is false it exits the loop, and in this case, if the statement is false B must be greater than or equal to A. The Repeat loop is exactly the opposite, it loops while the statement is false, and then exits when it becomes true. I like to think of the Repeat loop as "repeat the loop until the statement is true". So if we had the A>B statement again, using the Repeat loop would be the same as telling the program to "repeat the loop until A is greater than B". Here's an example, though I don't think it's necessary:

:Repeat A>B
:Disp B
:Disp A
:Prompt A
:Prompt B
:End
:Disp "A IS NOW GREATER","THAN B"

Sorry if any of this is confusing, I tried to explain as best as I could. Remember, click the comments/suggestions/ideas link if you have any questions. Now, on to the Goto statement, which is actually a branching statement, but it could make a good looping statement if you wanted it to.

Goto and Lbl:
The Goto statement does exactly what it says, it goes to somewhere else in the program. The Lbl statement tells the program where that "somewhere else" is. In order to use Goto and Lbl you must declare the Lbl as a letter or number, such as Lbl A. Then when you want to go to that label, all you need to do is say Goto A. Pretty simple, huh? Now that I'm on the subject, I want to warn you about using the Goto statement inside of a conditional or a loop: If you're using the If, Then statements never EVER use Goto inside of it. The reason is, if you go somewhere else in the program then the If statement never reaches its own End statement. At first it may not seem like there's anything wrong, but the program will slow down and eventually stop if this happens 100 times in a program, believe me, I've had it happen. The If statement isn't the only time this can happen, though. Any time there's a statement that requires an End you must never put a Goto inside of it, for example, For, While, or Repeat. If you would like to use the If statement to go somewhere else, make sure you don't use Then, just use the colon method from the last section:

:If A=10:Goto B

With all this, you now have the ability to loop anything you want and to branch. At this point, you should be able to convert any simple program outline into the BASIC language, or at least have the knowledge to know what statements you will need to use to get the job done. I don't believe I've talked enough about variables and actually writing a program in order for you to feel 100% comfortable with writing a program on your own. The next couple sections will put the finishing touches to your basic knowledge of writing programs in BASIC.

Section 4 | Index

Comments, Questions, or Ideas: Click Here | Home

Copyright © 2004 HeaneySoft