Section 1.5---If-then statements
Section 1.5---If-then statements

Back to home

If then statements work like this: If the variable you request equals what you say, then do the following lines of code. If not, then skip the following lines of code until an "End" is reached. It works like this:
:If A=1
:Then
:Disp "Hey"
:Pause
:End
:If A=2
:Then
:Disp "Bye"
:Pause
:End
If you have ONLY ONE LINE OF CODE under the If, you don't need the "Then" and the "End", for example, This:
:If A=1
:Then
:Disp "Hey"
:End
Could be replaced by this:
:If A=1
:Disp "Hey"
This has the same effect, but will only work on one line! If you have more than one line inside the if-then, be sure to use then and end. For the equals sign, press [2nd],[Test (Math)],[Enter].
Be
Careful
Be careful about putting If-Then statements inside other If-Then statements. Although this works just fine, it soon gets extremely confusing, and usually one more or one less "End" gets put at the end than needed, causing a syntax error, or for the program to skip the rest of the code.
If then statements are very helpful, and you will use them many more times in your programming. Let's try writing a program. This program should ask you for a number, then, if the number equals 1, display "Hi!", and if the number equals 2, display "Bye!". See below for what it should look like, but try to write it yourself before you look at the answer.
:Input "What?",A
:If A=1
:Disp "Hi!"
:If A=2
:Disp "Bye!"
If you want it to do something after all the if-then statements, you know how to do this, but we'll remind you anyway. Put the commands that you always want to be executed after every If-Then statement after all the statements. Basically, add this on to the end of the program you just wrote:
:Pause
:ClrHome
Pause is a command that will wait for you to press enter before it goes on with the rest of the program. It is obtained by pressing [PRGM],[8]. ClrHome clears the home screen. It is obtained by pressing [PRGM],[Right Arrow],[8]. Run the program. It should now ask you for a number. Type in 1 or 2. It should either say "Hi!" or "Bye!", and then wait for you to press enter. After you press enter, it should clear the homescreen, and say "Done".
Section 1.6---While Loops