Flow of Control

Flow of control refers to the order in which the statements in a program are executed. Python provides various control structures that allow you to control the flow of your program based on conditions and loops.

1. Conditional Statements

Conditional statements allow you to execute certain blocks of code based on whether a condition is true or false.

1.1 if Statement

The if statement is used to execute a block of code only if a specified condition is true.

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

1.2 if-else Statement

The if-else statement allows you to execute one block of code if the condition is true and another block if it is false.

# if-else statement x = 3 if x > 5: print("x is greater than 5") else: print("x is not greater than 5")

1.3 if-elif-else Statement

The if-elif-else statement allows you to check multiple conditions and execute different blocks of code accordingly.

# if-elif-else statement marks = 75 if marks > 90: grade = "A" elif marks > 80: grade = "B" elif marks > 70: grade = "C" elif marks > 60: grade = "D" else: grade = "F" print(f"Grade: {grade}")

1.4 Nested if Statements

You can also nest if statements inside other if statements to check for multiple conditions.

# Nested if statement x = 10 y = 5 if x > 5: print("x is greater than 5") if y < 10: print("y is less than 10")

2. Looping Statements

Loops are used to execute a block of code repeatedly as long as a condition is true or for a specified number of times.

2.1 for Loop

The for loop is used to iterate over a sequence (like a list, tuple, string) or other iterable objects.

# for loop with a list fruits = ["apple", "banana", "cherry"] for fruit in fruits: print(fruit) # for loop with range for i in range(5): print(i) # Prints 0, 1, 2, 3, 4

2.2 while Loop

The while loop executes a block of code as long as a specified condition is true.

# while loop count = 0 while count < 5: print(count) count += 1 # Increment count

3. Loop Control Statements

Python provides statements to control the flow of loops.

3.1 break Statement

The break statement is used to exit a loop prematurely.

# break statement for i in range(10): if i == 5: break # Exit the loop when i equals 5 print(i) # Prints 0, 1, 2, 3, 4

3.2 continue Statement

The continue statement is used to skip the current iteration of a loop and continue with the next iteration.

# continue statement for i in range(10): if i == 5: continue # Skip iteration when i equals 5 print(i) # Prints 0, 1, 2, 3, 4, 6, 7, 8, 9

3.3 pass Statement

The pass statement is a null operation; nothing happens when it executes. It is used as a placeholder.

# pass statement for i in range(5): if i == 3: pass # Do nothing when i equals 3 print(i) # Prints 0, 1, 2, 3, 4

Practice Questions

  1. Write a Python program to check if a number is positive, negative, or zero.
  2. Write a Python program to find the largest among three numbers using nested if statements.
  3. Write a Python program to print all even numbers between 1 and 20 using a for loop.
  4. Write a Python program to calculate the factorial of a number using a while loop.
  5. Write a Python program to print the Fibonacci sequence up to a given number using loops.

Related Resources

Need Help?

Join our tuition classes for personalized guidance and doubt clearing.

Register for Classes →