if...else Statement
Last updated
Was this helpful?
Last updated
Was this helpful?
Decision making is required when we want to execute a code only if a certain condition is satisfied.
The if…elif…else
statement is used in Python for decision making.
Here, the program evaluates the test expression
and will execute statement(s) only if the test expression is True
.
If the test expression is False
, the statement(s) is not executed.
In Python, the body of the if
statement is indicated by the indentation. The body starts with an indentation and the first unindented line marks the end.
Python interprets non-zero values as True
. None
and 0
are interpreted as False
.
Flowchart of if statement in Python programming
When you run the program, the output will be:
In the above example, num > 0
is the test expression.
The body of if
is executed only if this evaluates to True
.
When the variable num is equal to 3, test expression is true and statements inside the body of if
are executed.
If the variable num is equal to -1, test expression is false and statements inside the body of if
are skipped.
The print()
statement falls outside of the if
block (unindented). Hence, it is executed regardless of the test expression.
The if..else
statement evaluates test expression
and will execute the body of if
only when the test condition is True
.
If the condition is False
, the body of else
is executed. Indentation is used to separate the blocks.
Flowchart of if...else statement in Python
Output
In the above example, when num is equal to 3, the test expression is true and, the body of if
is executed and the body
of else is skipped.
If num is equal to -5, the test expression is false, and the body of else
is executed and the body of if
is skipped.
If num is equal to 0, the test expression is true and, the body of if
is executed and body
of else is skipped.
The elif
is short for else if. It allows us to check for multiple expressions.
If the condition for if
is False
, it checks the condition of the next elif
block and so on.
If all the conditions are False
, the body of else is executed.
Only one block among the several if...elif...else
blocks is executed according to the condition.
The if
block can have only one else
block. But it can have multiple elif
blocks.
Flowchart of if...elif....else statement in Python
When variable num is positive, Positive number is printed.
If num is equal to 0, Zero is printed.
If num is negative, Negative number is printed.
We can have a if...elif...else
statement inside another if...elif...else
statement. This is called nesting in computer programming.
Any number of these statements can be nested inside one another. Indentation is the only way to figure out the level of nesting. They can get confusing, so they must be avoided unless necessary.
Output 1
Output 2
Output 3