Comparision operators | ||||||||||||||||||||||
If statements always require a condition which will evaluate to true or false. This condition will perform some form of comparison. Consider the code below -
It is checking to see if the variable var is bigger than 4. So in this case the comparison we are doing is to check that var is greater than 4. This will be a true or false answer. Consider the next piece of code.
You may think that the text will be displayed. However you would be wrong! The text will not be displayed as the condition says less than and not less than or equal to. As var was assigned the value of 4 we are comparing to see is 4 is smaller than 4 which is false. A number can not be smaller than itself. There are other tests we can do. Here is a list of them
Ok, lets look at a secret number guessing program we want to create. The first test we want to do is to see if the number is too big. The variable val has been assigned to the number the user has guessed. The variable secret is the number the user is trying to guess. IF val > secret THEN PRINT "Sorry dude, the number is too big" END IF IF val < secret THEN PRINT "Sorry dude, the number is too small" END IF
We have two tests now. If the number is too big we run the code inside the first if statement. If too small then we run the code inside the second if statement. It should not be too hard to imagine how you could add code to test for equals. |