Master the three fundamental control structures: sequence, selection, and iteration. Learn how to control program flow and make decisions in your code.
Instructions executed one after another in a specific order
Simple calculations, data input/output, basic program setup, linear processes
# Sequence - steps executed in order
print("Welcome to the calculator")
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
result = num1 + num2
print(f"The sum is: {result}")
print("Thank you for using the calculator")
# Each line executes after the previous one completesChoose different paths based on conditions and decision points
User input validation, different outcomes based on conditions, menu systems, error handling
# Selection - if/elif/else statements
age = int(input("Enter your age: "))
if age < 13:
category = "child"
price = 5.00
elif age < 65:
category = "adult"
price = 10.00
else:
category = "senior"
price = 7.50
print(f"Category: {category}, Price: £{price}")
# Match-case (Python 3.10+)
match category:
case "child":
print("Special kids menu available!")
case "adult":
print("Full menu access")
case "senior":
print("Senior discount applied")Repeat instructions while conditions are true or for specific counts
Processing lists, repetitive calculations, user input validation, counting operations
# For loop - known number of iterations
print("Countdown:")
for i in range(5, 0, -1):
print(i)
print("Launch!")
# For loop with collections
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
print(f"I like {fruit}")
# While loop - condition-based
password = ""
attempts = 0
max_attempts = 3
while password != "secret123" and attempts < max_attempts:
password = input("Enter password: ")
attempts += 1
if password == "secret123":
print("Access granted!")
else:
print("Too many failed attempts")if condition:if age >= 18:
print("Can vote")Single condition check
if condition:
# code
else:
# codeif score >= 60:
print("Pass")
else:
print("Fail")Two alternative outcomes
if condition1:
elif condition2:
else:if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
else:
grade = "C"Multiple conditions
match variable:
case value1:
case value2:match day:
case "Monday":
print("Start of week")
case "Friday":
print("TGIF!")Multiple exact value matches
Known number of iterations
for i in range(start, stop, step):for i in range(1, 11):
print(i)Automatically after specified count
Processing each item in a list
for item in collection:for name in names:
print(f"Hello {name}")After processing all items
Condition-based repetition
while condition:while user_input != "quit":
user_input = input("Command: ")When condition becomes False
Execute at least once
while True:
# code
if not condition:
breakwhile True:
choice = input("Continue? (y/n): ")
if choice == "n":
breakBreak statement when condition met
Design programs using different combinations of sequence, selection, and iteration
Compare for loops vs while loops for different scenarios and optimize performance
Create programs with nested loops and conditional statements