Control Flow

George's Python
4 min readFeb 24, 2023

--

Control Flow is an essential part of any programming language, and it refers to the way a program decides which instructions to execute based on certain conditions. In this lecture, we will focus on three main aspects of control flow: if statements, for loops, and while loops.

Photo by Florian Olivo on Unsplash

If Statements:

If statements allow us to execute certain code only if a certain condition is true. The syntax for an if statement is as follows:

if condition:
# code to be executed if the condition is true

For example:

x = 10
if x > 5:
print("x is greater than 5")

In this example, the program will only print “x is greater than 5” if x is indeed greater than 5.

We can also add an else statement to an if statement, which will execute if the condition is false:

if condition:
# code to be executed if the condition is true
else:
# code to be executed if the condition is false

For example:

x = 2
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")

In this example, the program will print “x is not greater than 5” because x is not greater than 5.

We can also use elif statements to check for multiple conditions:

if condition1:
# code to be executed if condition1 is true
elif condition2:
# code to be executed if condition2 is true
else:
# code to be executed if neither condition1 nor condition2 are true

For example:

x = 10
if x > 15:
print("x is greater than 15")
elif x > 5:
print("x is greater than 5 but less than or equal to 15")
else:
print("x is less than or equal to 5")

In this example, the program will print “x is greater than 5 but less than or equal to 15” because x is greater than 5 but less than or equal to 15.

For Loops:

For loops allow us to execute certain code for each element in an iterable (such as a list or string). The syntax for a for loop is as follows:

for element in iterable:
# code to be executed for each element in the iterable

For example:

fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)

In this example, the program will print “apple”, “banana”, and “cherry” on separate lines.

We can also use range() to loop a specific number of times:

for i in range(5):
print(i)

In this example, the program will print the numbers 0 through 4 on separate lines.

While Loops:

While loops allow us to execute certain code as long as a certain condition is true. The syntax for a while loop is as follows:

while condition:
# code to be executed as long as the condition is true

For example:

i = 1
while i < 6:
print(i)
i += 1

In this example, the program will print the numbers 1 through 5 on separate lines.

It is important to note that while loops can potentially run forever if the condition is never false, so it is important to ensure that the condition will eventually be false.

Practice:

Now that we have covered the basics of control flow in Python, let’s practice with some examples.

  1. If Statements:

Example 1: Check if a number is positive or negative

num = -5

if num >= 0:
print("The number is positive")
else:
print("The number is negative")
num = -5

if num >= 0:
print(">!Example 1: The output will be "The number is negative", since -5 is less than 0.!<")
else:
print(">!Example 1: The output will be "The number is negative", since -5 is less than 0.!<")

Answer

Example 2: Check if a number is even or odd

num = 10

if num % 2 == 0:
print("The number is even")
else:
print("The number is odd")

Answer

2. For Loops:

Example 1: Print all the even numbers between 1 and 10

for i in range(1, 11):
if i % 2 == 0:
print(i)

Answer

Example 2: Iterate over a list of names and print a message for each name

names = ["Alice", "Bob", "Charlie", "Dave"]

for name in names:
print(f"Hello, {name}!")

Answer

  1. While Loops:

Example 1: Print all the numbers between 1 and 10 using a while loop

num = 1

while num <= 10:
print(num)
num += 1

Answer

Example 2: Ask the user to guess a number between 1 and 10, and keep prompting until they guess correctly

secret_number = 7
guess = 0

while guess != secret_number:
guess = int(input("Guess the secret number (between 1 and 10): "))

if guess < secret_number:
print("Too low, try again!")
elif guess > secret_number:
print("Too high, try again!")
else:
print("Congratulations, you guessed the secret number!")

Answer

These are just a few examples, but you can come up with your own practice exercises using if, for, and while statements to improve your understanding of control flow in Python.

for the Next step Click here

--

--