Free Web Hosting Provider - Web Hosting - E-commerce - High Speed Internet - Free Web Page
Search the Web

TI Source: Conditional Statements
 
 Path: Home - Basic - Conditional Statements    
  Basic    Assembly    Interactive    Miscellaneous    Members  

  Guide    Glossary    Programs  
 Conditional StatementsBy Justin 

A conditional statement is a very useful tool when programming. It is what you use to test two values. For example, if you want to test a variable to see if it equals 3, then you would use a conditional statement like this:

If the condition is true, then the command on the following line will be executed. If the condition is false, then the next line will be skipped.

So, if A did equal three, then the number 3 would be displayed. If A did not equal 3, then the next line would be skipped.

Program example:


Breakdown of code:

ClrHome
Clear the home screen.
Disp "AGE"
Display the text.
Input A
Prompt the user to input a value to the variable A. (This should be their age).
If A<11
Test to see if A is greater than 10. If A is greater than 10, then the condition is true and the next line will be executed. If A is not greater than 10, then the condition is false and the next line will be skipped.
Disp "YOU ARE LITTLE"
Display the text. (This line will be executed if A<10).
If A>10 and A<20
This will tell the calculator to test A twice. This condition has two parts to it. In order for it to be true, A must be greater than 10 and less than 20.
Disp "GETTING OLDER..."
Display the text. (Only if the above condition is true).
If A>20
Test A to see if it is greater than 20.
Disp "YOU ARE OLD"
Display the text. (Only if A is greater than 20).

Output:

The above example shows the most basic conditional statement. If the condition is true you can only execute one line of code. What if you want to run many lines of code? Well, that is quite simple to do.

Just insert Then before the code to be executed and End after the code. Like the following example:


Notice where the Then and End commands are.

Now for one last command. The Else command. Else is a very useful command. I will start out with an example:


Breakdown of code:

ClrHome
Clear the home screen.
randInt(1,100) -> R
Generate a random integer between 1 and 100. Store it in the variable R.
If R<50
Test R to see if it is less than 50.
Then
If the above condition is true, then The code between Then and Else will be executed. If the condition is false the code between Else and End will be executed.
Disp R
Display the value stored in R. (This will only happen if the condition is true)
Else
...Or else... (If the condition is false)
Disp "TOO HIGH"
Display the text. (This text will be displayed if the condition is false)
End
End the If - Then statement.

Output:

  or  

Program ideas:

Now that you are an expert on conditional statements, try this:

Make a guessing game. Have the calculator randomly pick a number, then the user must guess it. Tell the user if the guess is too high, or too low. Maybe even give them 10 guesses before they lose.

 Downloads:
TitleDescriptionAuthor
ifthen.zipAll filesJustin
ifthen_age.zipAsk the user for their age.Justin
ifthen_age2.zipAll filesJustin
ifthen_guess.zip- Program idea -Justin
ifthen_rand.zipDisplay either the random number or "TOO HIGH."Justin

  Basic    Assembly    Interactive    Miscellaneous    Members