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 -


if var > 4: 
	print "var is greater than 4. Yeah baby"

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.


var = 4
if var < 4: 
	print "var is less than 4. Yeah baby!"

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

Test

Symbol used

Example

Testing for greater than

>  

A > 5 (A is greater than 5)

Testing for less than

<  

A < 5 (A is less than 5)

Greater or equal to

>=

A >= 5 (A is greater or equal to 5)

Less than or equal to

<=

A <= 5 (A is less than or equal to 5)

Equals

=

A = 5 (A equals 5)

Not equals

<>  

A <> 5 (A does not equal 5)

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


if val > secret: 
	print "Sorry dude, the number is too big" 

if val < secret: 
	print "Sorry dude, the number is too small"

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.