2.3 Control Flow

Control flow refers to the order in which statements are executed in a program. It allows us to control the flow of execution based on certain conditions or criteria. In Python, control flow is achieved through the use of conditional statements and loops.

Conditional Statements

Conditional statements allow us to execute different blocks of code based on certain conditions. The most commonly used conditional statements in Python are the if, elif, and else statements.

The if statement is used to execute a block of code if a certain condition is true. Here's the basic syntax of an if statement:

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

The elif statement is used to check additional conditions if the previous conditions are not met. It stands for "else if". Here's the syntax of an elif statement:

if condition1:
    # code to be executed if condition1 is true
elif condition2:
    # code to be executed if condition2 is true

The else statement is used to execute a block of code if none of the previous conditions are true. It is optional and can only appear after an if or elif statement. Here's the syntax of an else statement:

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 none of the conditions are true

Let's look at an example to understand how conditional statements work in Python:

age = 25

if age < 18:
    print("You are a minor.")
elif age >= 18 and age < 65:
    print("You are an adult.")
else:
    print("You are a senior citizen.")

In this example, the program checks the value of the age variable and prints a corresponding message based on the condition that is true.

Loops

Loops allow us to repeatedly execute a block of code until a certain condition is met. Python provides two types of loops: for loops and while loops.

For Loops

A for loop is used to iterate over a sequence (such as a list, tuple, or string) or other iterable objects. It executes a block of code for each item in the sequence. Here's the syntax of a for loop:

for item in sequence:
    # code to be executed for each item

Let's look at an example to understand how for loops work in Python:

fruits = ["apple", "banana", "orange"]

for fruit in fruits:
    print(fruit)

In this example, the for loop iterates over each item in the fruits list and prints it.

While Loops

A while loop is used to repeatedly execute a block of code as long as a certain condition is true. It continues to execute the code until the condition becomes false. Here's the syntax of a while loop:

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

Let's look at an example to understand how while loops work in Python:

count = 0

while count < 5:
    print(count)
    count += 1

In this example, the while loop continues to execute as long as the value of count is less than 5. It prints the value of count and increments it by 1 in each iteration.

Control Flow Keywords

Python provides several keywords that can be used to control the flow of execution within conditional statements and loops.

The break keyword is used to exit a loop prematurely. It is often used with an if statement to check for a certain condition and break out of the loop if the condition is met.

The continue keyword is used to skip the rest of the code in the current iteration of a loop and move on to the next iteration.

The pass keyword is used as a placeholder when a statement is required syntactically but you don't want to execute any code. It is often used as a placeholder for future code implementation.

Conclusion

Control flow is an essential concept in programming, and Python provides powerful tools for controlling the flow of execution in a program. Conditional statements allow us to execute different blocks of code based on certain conditions, while loops allow us to repeatedly execute a block of code until a certain condition is met. By understanding and utilizing control flow in Python, you can write more efficient and flexible programs.