Condition Statements

 

All control flow tools involve evaluating a condition statement. The program will proceed differently depending on whether the condition is met.

 

The most common condition statement is the comparison statement. If we want to compare whether two variables are the same, we use the == sign (double =). For instance, if you write x == y, you are asking the program to check if the value of x is equals to the value of y. If they are equal, the condition is met and the statement will evaluate to True. Else, the statement will evaluate to False.

 

Other comparison signs include != (not equals), < (smaller than), > (greater than), <= (smaller than or equals to) and >= (greater than or equals to). The list below shows how these signs can be used and gives examples of statements that will evaluate to True.

 

Not equals:

5 != 2

 

Greater than:

5>2

 

Smaller than:

2<5

 

Greater than or equals to:

5>=2

5>=5

 

Smaller than or equals to:

2 <= 5

2 <= 2

 

We also have three logical operators, and, or, not that are useful if we want to combine multiple conditions.

 

The and operator returns True if all conditions are met. Else it will return False. For instance, the statement 5==5 and 2>1 will return True since both conditions are True.

 

The or operator returns True if at least one condition is met. Else it will return False. The statement 5 > 2 or 7 > 10 or 3 == 2 will return True since the first condition 5>2 is True.

 

The not operator returns True if the condition after the not keyword is false. Else it will return False. The statement not 2>5 will return True since 2 is not greater than 5.