A decision involves selecting from one of the two or more possible options.
The if statement is used for selection or decision making.
Conditional Statements
1. If statement
Syntax:
if condition:
statement(s)
If the condition is true, then the indented statement(s) are executed. The indentation implies that its execution is dependent on the condition.
Example program:
2. If….else statement
Syntax:
if condition:
statement(s)
else:
statement(s)
If the condition is true, then the first indented statement(s) are executed. If the condition is false, then the second indented statement(s) are executed.
Example program:
3. If…elif…else
Syntax:
if condition:
statement(s)
elif condition:
statement(s)
elif condition:
statement(s)
else:
statement(s)
Number of elif is dependent on the number of conditions to be checked. If the first condition is false, then the next condition is checked, and so on. If one of the conditions is true, then the corresponding indented block executes, and the if statement terminates.
Example program: