Python Control Flow Statements


Control Flow Statements in Python

Control flow statements in Python dictate the order in which the statements are executed in a program. They allow you to execute different blocks of code based on certain conditions, creating a more dynamic and responsive program. The main types of control flow statements in Python include:

  1. Conditional Statements
  2. Looping Statements
  3. Control Statements (break, continue, pass)

1. Conditional Statements

Conditional statements allow you to execute certain pieces of code based on whether a condition is true or false. The primary conditional statements in Python are if, elif, and else.

Syntax

if condition: # code to execute if condition is true elif another_condition: # code to execute if another_condition is true else: # code to execute if both conditions are false

Example

x = 10 if x > 0: print("x is positive") elif x < 0: print("x is negative") else: print("x is zero")

2. Looping Statements

Looping statements are used to repeat a block of code multiple times. The primary looping statements in Python are for and while.

a. For Loop

The for loop iterates over a sequence (like a list, tuple, string, or dictionary).

Syntax
for variable in sequence: # code to execute
Example
fruits = ['apple', 'banana', 'cherry'] for fruit in fruits: print(fruit)

b. While Loop

The while loop continues to execute as long as a specified condition is true.

Syntax

while condition: # code to execute
Example
count = 0 while count < 5: print(count) count += 1

3. Control Statements

Control statements modify the flow of a loop.

a. Break Statement

The break statement is used to exit a loop prematurely.

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

b. Continue Statement

The continue statement skips the current iteration and moves to the next iteration of the loop.

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

c. Pass Statement

The pass statement is a null operation; it’s a placeholder that does nothing when executed. It can be useful in situations where a statement is syntactically required but no action is needed.

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

Summary of Control Flow Statements

  1. Conditional Statements:

    • if: Executes a block of code if the condition is true.
    • elif: Executes a block of code if the previous conditions were false and this condition is true.
    • else: Executes a block of code if all previous conditions are false.
  2. Looping Statements:

    • for: Iterates over a sequence.
    • while: Continues to execute as long as the condition is true.
  3. Control Statements:

    • break: Exits the loop prematurely.
    • continue: Skips to the next iteration of the loop.
    • pass: A null operation; acts as a placeholder.

These control flow statements provide the building blocks for structuring and managing the flow of your Python programs effectively.