Control Flow Statements and Loops
Control Flow Statements and Loops
In Python programming, flow control is the order in which statements or blocks of code are executed at runtime based on a
condition.
1. Conditional statements
2. Iterative statements.
3. Transfer statements
Conditional statements
In Python, condition statements act depending on whether a given condition is true or false. You can execute different blocks of
codes depending on the outcome of a condition. Condition statements always evaluate to either True or False.
For example, assigning grades (A, B, C) based on marks obtained by a student.
1. if the percentage is above 90, assign grade A
2. if the percentage is above 75, assign grade B
3. if the percentage is above 65, assign grade C
In Python, there are three forms of the if...else statement.
1. if statement
2. if...else statement
3. if...elif...else statement
Python if statement
The syntax of if statement in Python is:
if condition:
# body of if statement
Working of if Statement
In the above example, we have created a variable named number. Notice the test condition,
number > 0
number = -5
This is because the value of number is less than 0. Hence, the condition evaluates to False. And, the body of if block is skipped.
Python if...else Statement
An if statement can have an optional else clause.
The syntax of if...else statement is:
if condition:
# block of code if condition is True
else:
# block of code if condition is False
if number > 0:
print('Positive number')
else:
print('Not a Positive number')
number > 0
Since the value of number is 10, the test condition evaluates to True. Hence code inside the body of if is executed.
If we change the value of variable to a negative integer. Let's say -5.
number = -5
Here, the test condition evaluates to False. Hence code inside the body of else is executed.
Python if...elif...else Statement
The if...else statement is used to execute a block of code among two alternatives.
However, if we need to make a choice between more than two alternatives, then we use the if...elif...else statement.
The syntax of the if...elif...else statement is:
if condition1:
# code block 1
elif condition2:
# code block 2
else:
# code block 3
Here,
1. If condition1 evaluates to true, code block 1 is executed.
2. If condition1 evaluates to false, then condition2 is evaluated.
a. If condition2 is true, code block 2 is executed.
b. If condition2 is false, code block 3 is executed.
if number > 0:
print("Positive number")
elif number == 0:
print('Zero')
else:
print('Negative number')
Zero
This statement is always executed
In the above example, we have created a variable named number with the value 0. Here, we have two condition expressions:
Here, both the conditions evaluate to False. Hence the statement inside the body of else is executed.
Python Nested if statements
We can also use an if statement inside of an if statement. This is known as a nested if statement.
The syntax of nested if statement is:
# outer if statement
if condition1:
# statement(s)
# inner if statement
if condition2:
# statement(s)
Notes:
• We can add else and elif statements to the inner if statement as required.
• We can also insert inner if statement inside the outer else or elif statements(if they exist)
• We can nest multiple layers of if statements.
# outer if statement
if (number >= 0):
# inner if statement
if number == 0:
print('Number is 0')
# inner else statement
else:
print('Number is positive')