Python Flow Control – Tutorial with Examples

Flow control statements in programming are used to control the order in which statements are executed in a program. These statements help in making decisions, repeating operations, and executing statements based on certain conditions. Python provides several control structures for controlling the flow of execution of a program.

Conditional Statements

Conditional statements are used to execute a certain code block only when a certain condition is met. Python provides two main conditional statements: if and if-else statements.

If Statement

The if statement is used to execute a code block only if the specified condition is true. The syntax of the if statement is as follows:

if condition:
    # Execute this block if the condition is true

For example, consider the following code that checks if a number is positive or negative:

number = int(input("Enter a number: "))
if number >= 0:
    print(number, "is a positive number.")

The output of the above code will be:

Enter a number: 10
10 is a positive number.

If-Else Statement

The if-else statement is used to execute a code block if the specified condition is true, and another code block if the condition is false. The syntax of the if-else statement is as follows:

if condition:
    # Execute this block if the condition is true
else:
    # Execute this block if the condition is false

For example, consider the following code that checks if a number is even or odd:

number = int(input("Enter a number: "))
if number % 2 == 0:
    print(number, "is an even number.")
else:
    print(number, "is an odd number.")

The output of the above code will be:

Enter a number: 7
7 is an odd number.

Loop Statements

Loop statements are used to execute a code block multiple times. Python provides two main loop statements: for and while loops.

For Loop

The for loop is used to iterate over a sequence of elements (such as a list, string, tuple, etc.). The syntax of the for loop is as follows:

for variable in sequence:
    # Execute this block for each element in the sequence

For example, consider the following code that prints all elements in a list:

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

The output of the above code will be:

apple
banana
cherry

While Loop

The while loop is used to execute a code block as long as the specified condition is true. The syntax of the while loop is as follows:

while condition:
    # Execute this block while the condition is true

For example, consider the following code that prints numbers from 1 to 5:

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

The output of the above code will be:

1
2
3
4
5

Control Flow Statements

Control flow statements are used to transfer the control flow of a program from one place to another. Python provides two main control flow statements: break and continue statements.

Break Statement

The break statement is used to exit a loop prematurely. When a break statement is executed inside a loop, the loop is terminated immediately and the control is transferred to the next statement after the loop. The syntax of the break statement is as follows:

break

For example, consider the following code that prints numbers from 1 to 5, but stops after printing 3:

for i in range(1, 6):
    if i == 3:
        break
    print(i)

The output of the above code will be:

1
2

Continue Statement

The continue statement is used to skip the current iteration of a loop and move to the next iteration. The syntax of the continue statement is as follows:

continue

For example, consider the following code that prints only the odd numbers from 1 to 5:

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

The output of the above code will be:

1
3
5

Conclusion

In this article, we have discussed the various flow control statements in Python, including conditional statements, loop statements, and control flow statements. These statements play an important role in controlling the flow of execution of a program and help in making decisions, repeating operations, and executing statements based on certain conditions. Understanding these flow control statements is essential for anyone who wants to learn Python programming.

Leave a Reply

Your email address will not be published. Required fields are marked *