Department of Computer Science
School of Informa4on & Communica4on Technology
Federal University of Technology Minna
CPT 211: Computer Programming I
Lecture Note 4 : Flow Control in Python
I. Introduc4on
Flow control is a fundamental concept in programming that allows the execu8on of code to
be altered based on certain condi8ons. Python provides several flow control statements to
control the flow of execu8on, including if-else statements, loops, and other control structures.
This lecture note will cover the various flow control mechanisms in Python and provide
detailed and concrete examples to illustrate the concepts.
II. If-else Statements
The if statement allows you to execute a block of code only if a specified condi8on is true.
Syntax:
if condition:
# code block to execute if condition is true
else:
# code block to execute if condition is false
Example 1: Checking if a number is posi8ve or nega8ve
num = int(input("Enter a number: "))
if num > 0:
print("The number is positive.")
else:
print("The number is negative.")
Example 2: Checking if a number is even or odd
num = int(input("Enter a number: "))
if num % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
Example 3: Checking if a student's grade is pass or fail
score = int(input("Enter the student's score: "))
if score >= 40:
print("The student has passed.")
else:
print("The student has failed.")
Example 4: Checking if a user input matches a predefined password
password = "secret"
user_input = input("Enter the password: ")
if user_input == password:
print("Access granted.")
else:
print("Access denied.")
III. Elif Statement
-1-
The elif statement allows you to check for mul8ple condi8ons in a chain and execute different
blocks of code based on the first condi8on that is true.
Syntax:
if condition1:
# code block to execute if condition1 is true
elif condition2:
# code block to execute if condition2 is true and condition1 is false
else:
# code block to execute if all previous conditions are false
Example 5: Assigning leHer grades based on a student's score
score = int(input("Enter the student's score: "))
if score >= 70:
print("Grade: A")
elif score >= 60:
print("Grade: B")
elif score >= 50:
print("Grade: C")
elif score >= 45:
print("Grade: D")
elif score >= 40:
print("Grade: E")
else:
print("Grade: F")
IV. Loops
Loops are used to repeatedly execute a block of code un8l a specified condi8on is met. Python
provides two types of loops: for loop and while loop.
1. For Loop
The for loop iterates over a sequence (such as a list, tuple, or string) or other iterable
objects.
Syntax:
for item in sequence:
# code block to execute
Example 6: Prin8ng elements of a list
numbers = [1, 2, 3, 4, 5]
for num in numbers:
print(num)
2. While Loop
The while loop repeatedly executes a block of code as long as a specified condi8on is
true.
Syntax:
while condition:
# code block to execute
Example 7: Coun8ng down from 5 to 1 using a while loop
num = 5
while num > 0:
print(num)
num -= 1
-2-
Example 8: Finding the sum of numbers using a while loop
num_sum = 0
num = 1
while num <= 10:
num_sum += num
num += 1
print("Sum:", num_sum)
V. Control Structures
Python provides addi8onal control structures to modify the flow of execu8on.
1. Break Statement.
The break statement is used to exit a loop prematurely.
Example: Finding the first even number in a list
numbers = [1, 3, 5, 6, 7, 8, 9]
for num in numbers:
if num % 2 == 0:
print("The first even number is:", num)
break
2. Con4nue Statement.
The con8nue statement is used to skip the rest of the code block and move to the next
itera8on of the loop.
Example: Prin8ng odd numbers in a list
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for num in numbers:
if num % 2 == 0:
continue
print(num)
VI. Nes4ng Flow Control Statements
Flow control statements can be nested within each other to create more complex decision-
making scenarios.
Example: Checking a number for divisibility by 2 and 3
num = int(input("Enter a number: "))
if num % 2 == 0:
if num % 3 == 0:
print("The number is divisible by both 2 and 3.")
else:
print("The number is divisible by 2 but not by 3.")
else:
print("The number is not divisible by 2.")
VII. Conclusion
Flow control is essen8al for controlling the execu8on of code based on certain condi8ons.
Python provides if-else statements, loops (for and while), and control structures like break and
con8nue to facilitate flow control.
By understanding and u8lizing these flow control mechanisms effec8vely, you can write more
efficient and versa8le Python programs.
-3-