UNIT -2
CONTROL STAMENTS
Types of Control Flow in Python
Python control flow statements are as follows:
1. The if statement
2. The if-else statement
3. The nested-if statement
4. The if-elif-else ladder
Python if statement
The if statement is the most simple decision-making statement. It is used to
decide whether a certain statement or block of statements will be executed or not.
Syntax:
if condition:
# Statements to execute if
# condition is true
Here, the condition after evaluation will be either true or false. if the statement
accepts boolean values – if the value is true then it will execute the block of
statements below it otherwise not.
Flowchart of Python if statement
,Example of Python if Statement
I=10
If(i>15);
Print(“10 is less than 15”)
Print (“I am Not in if”)
Output:
I am Not in if
Python If-Else Statement
The if statement alone tells us that if a condition is true it will execute a block of
statements and if the condition is false it won’t.
But if we want to do something else if the condition is false, we can use the else
statement with the if statement to execute a block of code when the if condition is
false.
The block of code following the else statement is executed as the condition
present in the if statement is false after calling the statement which is not in the
block(without spaces).
Syntax of Python If-Else:
if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false
Flowchart of Python if-else statement
, Example for If..Else:
i = 20
if (i < 15):
print("i is smaller than 15")
print("i'm in if Block")
else:
print("i is greater than 15")
print("i'm in else Block")
print("i'm not in if and not in else Block")
Output:
i is greater than 15
i'm in else Block
i'm not in if and not in else Block
Nested-If Statement in Python
A nested if is an if statement that is the target of another if statement. Nested if
statements mean an if statement inside another if statement.
Python allows us to nest if statements within if statements. i.e., we can place an if
statement inside another if statement.
Syntax:
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here
, Example of Python Nested if statement
i = 10
if (i == 10):
# First if statement
if (i < 15):
print("i is smaller than 15")
# Nested - if statement
# Will only be executed if statement above
# it is true
if (i < 12):
print("i is smaller than 12 too")
else:
print("i is greater than 15")
Output:
i is smaller than 15
i is smaller than 12 too
Python if-elif-else Ladder
The if statements are executed from the top down.
As soon as one of the conditions controlling the if is true, the statement associated
with that if is executed, and the rest of the ladder is bypassed.
If none of the conditions is true, then the final “else” statement will be executed.
Syntax:
if (condition):
statement
elif (condition):
statement
else:
statement
Flowchart of Python if-elif-else ladder
CONTROL STAMENTS
Types of Control Flow in Python
Python control flow statements are as follows:
1. The if statement
2. The if-else statement
3. The nested-if statement
4. The if-elif-else ladder
Python if statement
The if statement is the most simple decision-making statement. It is used to
decide whether a certain statement or block of statements will be executed or not.
Syntax:
if condition:
# Statements to execute if
# condition is true
Here, the condition after evaluation will be either true or false. if the statement
accepts boolean values – if the value is true then it will execute the block of
statements below it otherwise not.
Flowchart of Python if statement
,Example of Python if Statement
I=10
If(i>15);
Print(“10 is less than 15”)
Print (“I am Not in if”)
Output:
I am Not in if
Python If-Else Statement
The if statement alone tells us that if a condition is true it will execute a block of
statements and if the condition is false it won’t.
But if we want to do something else if the condition is false, we can use the else
statement with the if statement to execute a block of code when the if condition is
false.
The block of code following the else statement is executed as the condition
present in the if statement is false after calling the statement which is not in the
block(without spaces).
Syntax of Python If-Else:
if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false
Flowchart of Python if-else statement
, Example for If..Else:
i = 20
if (i < 15):
print("i is smaller than 15")
print("i'm in if Block")
else:
print("i is greater than 15")
print("i'm in else Block")
print("i'm not in if and not in else Block")
Output:
i is greater than 15
i'm in else Block
i'm not in if and not in else Block
Nested-If Statement in Python
A nested if is an if statement that is the target of another if statement. Nested if
statements mean an if statement inside another if statement.
Python allows us to nest if statements within if statements. i.e., we can place an if
statement inside another if statement.
Syntax:
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here
, Example of Python Nested if statement
i = 10
if (i == 10):
# First if statement
if (i < 15):
print("i is smaller than 15")
# Nested - if statement
# Will only be executed if statement above
# it is true
if (i < 12):
print("i is smaller than 12 too")
else:
print("i is greater than 15")
Output:
i is smaller than 15
i is smaller than 12 too
Python if-elif-else Ladder
The if statements are executed from the top down.
As soon as one of the conditions controlling the if is true, the statement associated
with that if is executed, and the rest of the ladder is bypassed.
If none of the conditions is true, then the final “else” statement will be executed.
Syntax:
if (condition):
statement
elif (condition):
statement
else:
statement
Flowchart of Python if-elif-else ladder