Python Control Flow Statements

Introduction

Control flow statements in Python allow you to direct the flow of execution in your programs. These statements enable you to execute code conditionally, repeatedly, or in a specific sequence, which is crucial for writing dynamic and flexible programs.

Types of Control Flow Statements

  1. Conditional Statements
  2. Looping Statements
  3. Control Statements

1. Conditional Statements

Conditional statements allow you to execute certain code based on whether a condition is True or False.

if Statement

Executes a block of code if the condition is True.

Syntax:

if condition:
    # block of code

Example:

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

if-else Statement

Executes a block of code if the condition is True, otherwise executes another block of code.

Syntax:

if condition:
    # block of code if condition is True
else:
    # block of code if condition is False

Example:

x = 10
if x > 15:
    print("x is greater than 15")
else:
    print("x is not greater than 15")

if-elif-else Statement

Allows you to check multiple conditions.

Syntax:

if condition1:
    # block of code if condition1 is True
elif condition2:
    # block of code if condition2 is True
else:
    # block of code if both conditions are False

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 5 or less")

2. Looping Statements

Looping statements allow you to execute a block of code repeatedly.

while Loop

Executes a block of code as long as the condition is True.

Syntax:

while condition:
    # block of code

Example:

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

for Loop

Iterates over a sequence (such as a list, tuple, string, or range) and executes a block of code for each item.

Syntax:

for item in sequence:
    # block of code

Example:

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

range in for Loop

The range function generates a sequence of numbers.

Example:

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

3. Control Statements

Control statements change the flow of execution within loops.

break Statement

Terminates the loop prematurely.

Example:

for i in range(5):
    if i == 3:
        break
    print(i)
# Output: 0 1 2

continue Statement

Skips the current iteration and proceeds to the next iteration of the loop.

Example:

for i in range(5):
    if i == 3:
        continue
    print(i)
# Output: 0 1 2 4

pass Statement

Does nothing; it can be used as a placeholder for future code.

Example:

for i in range(5):
    if i == 3:
        pass
    print(i)
# Output: 0 1 2 3 4

Conclusion

Control flow statements are essential for writing dynamic and flexible programs in Python. By understanding and using conditional statements (if, if-else, if-elif-else), looping statements (while, for), and control statements (break, continue, pass), you can control the flow of execution in your code effectively. These tools allow you to implement complex logic and repetitive tasks efficiently.

Leave a Comment

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

Scroll to Top