Conditional statements play a very important role in programming. Decisions are made through conditional statements.
We make various decisions every moment in our daily lives. If we have less money in our pockets, we spend carefully. On workdays, we spend busy hours. If there’s an important meeting, we dress up nicely—all these are parts of decision-making. Conditional statements are used for making such decisions in computers.
if Statement
The most common conditional statement is the if statement. This means we have to add a condition with if. Let’s see an example:
# This program is an example of if statement in python
weather=30
if(weather>28):
print("The weather is hot")
Result:
The weather is hot
Here, we set the temperature to 30 in a variable called weather
. We’ve imposed a condition with the if statement that if the temperature is above 28 degrees, it will show “The weather is hot.”
Note that there’s a colon (:) after the if condition, and the statement following the condition follows a specific indentation. Indentation is very important in Python. Beginners often make these two mistakes.
else Statement
In the previous example, we saw how to bring a condition into an if statement. But if the condition is not true, nothing will be displayed. For example:
weather=25
if(weather>28):
print("The weather is hot")
This code will not display any result because the condition is false. In such cases, to display a final message for unknown situations, we use the else statement. The following example will clarify this:
# This program is an example of else statement in python
weather=25
if(weather>28):
print("The weather is hot")
else:
print("The weather is unknown")
Result:
The weather is unknown
That is, only when the condition is false, the else statement will work.
elif Statement
In the above examples of if and else, we noticed that decisions can only be made in a yes or no manner, which is not realistic. The situation might be such that if the temperature is between 20 and 27, it is temperate, below 19 it is cold, and above 28 it is hot. For such conditions, where multiple conditions need to be linked together, we use the elif statement. Let’s see this example:
# This program is an example of elif statement in python
weather=25
if(weather<20):
print("The weather is cold.")
elif (weather>=20 and weather<=27):
print("The weather is temperate.")
elif(weather>27):
print("The weather is hot.")
Result:
The weather is temperate.
Here, all conditional operators apply for conditional statements, and complex conditions can be created by connecting multiple conditions with and
, or
, etc.
Nested if Statement
Nested conditions can be used within an if condition, which is called a nested condition. Let’s look at an example:
# This program is an example of nested conditions in python
weather=25
if(weather<20):
print("The weather is cold.")
else:
if (weather>=20 and weather<=27):
print("The weather is temperate.")
elif(weather>27):
print("The weather is hot.")
Result:
The weather is temperate.
Here, it works like the previous example, but first, we check if it’s cold weather. If it’s not, we move into the else part to check if it’s temperate or hot.
The if and elif statements used after else are nested conditions.