Conditional statements are one of the most fundamental concepts to understanding any programming language. Python programming language supports many conditional statements including if, else, and switch. Below we are going to discuss their syntax and working.
The if statement is used to check a condition and determine whether a block of code should be executed or not. The else statement is used to execute a different set of code when the value for the if statement isn’t true.
An if-else block looks like this:
if condition:
Statement(s)
else:
Statement(s)
Unlike other programming languages in Python, instead of curly brackets, the scope of the code is defined by indentation (white space at the start of a line).
We can use the following logical operators between two variables to determine the condition.
Equals: a == b
Not Equals: a != b
Less than: a < b
Greater than: a > b
Less than or equal to: a <= b
Greater than or equal to: a >= b
The following example checks if the count is greater than five.
Example 1.
# If the count is greater than 5, we print an message
count = 6
if count > 5:
print(count, "count is > 5 count")
print("Program Ended")
Output:
6 count is > 5 count
Program Ended
In the above program, we applied the if statement two times, at first-time count = 6 and condition in If the statement is greater than 6 (count > 6) is true, hence the print statement is executed.
The 2nd time, we change the value of the variable count to 3 and make it appear the same statement (count > 5) which is now False, hence print statement is not executed.
At the end “Program Ended” print statement is executed as it’s part of the if statement block.
If – Else Statement
The if statement is a block of code that executes if a certain condition is met, otherwise it does nothing. The else statement is a block of code that executes if the preceding if statement did not match, else it does nothing. Here is the syntax of if-else statement:
if (condition) {
statements1
}
else {
statements2
}
Example 2. (Adding Else Statement in the first example)
# If count is greater than 5, print message
count = 4
if count > 5:
print(count, " count is > 5")
Else:
print(count, " count < 5")
print( "Program Ended")
Output:
4 count is < 5
Program Ended
Elif Statement
With the elif keyword, Python tells the interpreter “try this condition if the previous conditions are not true”.
Let’s add Elif Statement to the previous example:
Example 3.
count = 4
if count > 5:
print(count, " count is > 5")
Elif count < 5:
print(count, " count is < 5")
Else:
print( "count = 5")
print("Program Ended")
Output:
4 count is < 5
Program Ended
In above example If condition (count>5) is false hence intended statements are not executed,
While in Elif condition (count<5) is true hence intended statement executed and the program control passed to below print statement.
If count = 5 on the first line of the program, what will the output be?
Let’s figure out this.
#check whether num is less than, greater than or equal
count = 5
if count > 5:
print(count, " count is greater than five")
Elif count < 5:
print(count, " count is less than five")
Else:
print( "count is five")
print("Program Ended")
Output:
count is five
Program Ended
If count is neither greater than 5, nor less than 5 it means count is five itself. To verify this you can use,
Elif count == 5:
Instead of the else statement, we can use the Elif statement multiple times in each block.
Pass Statement
It is not possible for if statements to be empty. However, if you have an empty if statement, put in a pass statement to avoid an error.
x = 346
y = 78
if y < x:
pass