Lecture Unit 4
Lecture Unit 4
Therefore, control flow statements makes the program to have a decision-making ability, so that
it performs different steps depending on different conditions.
Normally, there are two type of control flow statements in any programming language and Python
also supports them. Namely:
• Decision-making Statements
• Loops or Iteration Statements
2
What are Control Flow Statements?
Decision-making Statements
Decision making statements are used in the Python programs to make them able to decide
which of the alternative group of instructions to be executed, depending on value of a certain
Boolean expression.
3
What are Control Flow Statements?
Types of Decision-making Statements in Python
• The if statement
• The if-else statement
• The nested-if statement
• The if-elif-else ladder
• Switch statements (match)
4
What are Control Flow Statements?
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.
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.
5
What are Control Flow Statements?
As we know, python uses indentation to identify a block. So the block under an if statement
will be identified as shown in the below example:
6
What are Control Flow Statements?
Flowchart of Python if statement
Let’s look at the flow of code in the If statement
7
What are Control Flow Statements?
Example of Python if Statement
As the condition present in the if statement is false. So, the block below the if statement is executed.
8
What are Control Flow Statements?
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.
9
What are Control Flow Statements?
Flowchart of Python if-else statement
Let’s look at the flow of code in an if-else statement
10
What are Control Flow Statements?
11
What are Control Flow Statements?
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.
Yes, Python allows us to nest if statements within if statements. i.e., we can place an if statement
inside another if statement.
12
What are Control Flow Statements?
Flowchart of Python Nested if Statement
Let’s look at the flow of control in Nested if Statements
13
What are Control Flow Statements?
Example of Python Nested if statement
In this example, we are showing nested if conditions in the code, All the If conditions will be
executed one by one.
14
What are Control Flow Statements?
Python if-elif-else Ladder
Here, a user can decide among multiple options. 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.
15
What are Control Flow Statements?
Flowchart of Python if-elif-else ladder
Let’s look at the flow of control in if-elif-else ladder:
16
What are Control Flow Statements?
Example of Python if-elif-else ladder
In the example, we are showing single if condition, multiple elif conditions, and single
else condition.
17
What are Control Flow Statements?
Short Hand if statement
Whenever there is only a single statement to be executed inside the if block then shorthand if
can be used. The statement can be put on the same line as the if statement.
18
What are Control Flow Statements?
Example of Python if shorthand
In the given example, we have a condition that if the number is less than 15, then further code
will be executed.
19
What are Control Flow Statements?
Short Hand if-else statement
This can be used to write the if-else statements in a single line where only one statement is
needed in both the if and else blocks.
20
What are Control Flow Statements?
Example of Python if else shorthand
In the given example, we are printing True if the number is 15, or else it will print False.
21
What are Control Flow Statements?
Chaining comparison operators in Python
In Python, there is a better way to write this using the Comparison operator Chaining.
The chaining of operators can be written as follows:
22
What are Control Flow Statements?
Here’s an example of chaining comparison operators:
23
What are Control Flow Statements?
24
What are Control Flow Statements?
Switch Case in Python
In Python 3.10 and after that, Python will support this by using match in place of switch:
25
What are Control Flow Statements?
Loops or Iteration Statements
Most of the processes require a group of instructions to be repeatedly executed. In programming
terminology, it is called a loop. Instead of the next step, if the flow is redirected towards any
earlier step, it constitutes a loop.
26
What are Control Flow Statements?
Types of Loop Statements in Python
• For loop
• While loop
27
What are Control Flow Statements?
Flowchart of for loop
28
What are Control Flow Statements?
Examples of Python For Loop
29
What are Control Flow Statements?
Python While Loop
Python While Loop is used to execute a block of statements repeatedly until a given condition
is satisfied. When the condition becomes false, the line immediately after the loop in the program
is executed.
30
What are Control Flow Statements?
Flowchart of Python While Loop
31
What are Control Flow Statements?
Python While Loop
In this example, the condition for while will be True as long as the counter variable (count)
is less than 3.
32
Questions????
Perlongs Computing
33