Control structures are an essential part of any programming language, as they determine the flow of execution based on conditions and repetitions. In this section, we will explore conditional statements (if-else) and loops (for and while), which allow programmers to make decisions and repeat actions efficiently.
1. Conditional Statements (If, Elif, Else)
Conditional statements allow a program to make decisions by checking whether a specific condition is True or False. The execution of code depends on these conditions.
1.1 The if Statement
The if statement checks a condition and executes the block of code inside it if the condition is True.
Syntax:
if condition:
# Code block that runs if condition is True
Example:
temperature = 30
if temperature > 25:
print(“It’s a hot day!”) # This line runs if temperature is greater than 25
1.2 The if-else Statement
The else block allows an alternative set of instructions to run if the condition is False.
Syntax:
if condition:
# Code block executed if condition is True
else:
# Code block executed if condition is False
Example:
age = 16
if age >= 18:
print(“You are eligible to vote.”)
else:
print(“You are too young to vote.”) # Runs if age is less than 18
1.3 The if-elif-else Statement
When multiple conditions need to be checked, elif (short for else if) is used.
Syntax:
if condition1:
# Runs if condition1 is True
elif condition2:
# Runs if condition2 is True (only if condition1 is False)
else:
# Runs if none of the conditions above are True
Example:
score = 85
if score >= 90:
print(“Grade: A”)
elif score >= 75:
print(“Grade: B”) # This will run since score is between 75 and 89
else:
print(“Grade: C”)
2. Looping Constructs: Repeating Actions
Loops allow a program to repeat a block of code multiple times. The two main types of loops in Python are for loops and while loops.
2.1 The for Loop
A for loop is used to iterate through a sequence (e.g., a list, range, or string).
Syntax:
for variable in sequence:
# Code block that runs for each item in the sequence
Example 1: Looping Through a Range
for i in range(5):
print(i) # Prints 0, 1, 2, 3, 4
Here, range(5) generates numbers from 0 to 4, and the loop prints each number.
Example 2: Looping Through a List
fruits = [“apple”, “banana”, “cherry”]
for fruit in fruits:
print(fruit) # Prints each fruit name
2.2 The while Loop
A while loop continues running as long as a condition is True.
Syntax:
while condition:
# Code block that runs while the condition is True
Example:
count = 1
while count <= 5:
print(count) # Prints numbers from 1 to 5
count += 1 # Increases count by 1 in each loop iteration
⚠ Warning: If the condition in a while loop never becomes False, it results in an infinite loop which can crash the program.
3. Comparing Block Code (Scratch) to Python
Many students transitioning from Scratch to Python may find similarities in logic, though the syntax is different.
Example of an If-Else Condition in Scratch
- In Scratch, an if-else statement looks like:
if (score > 50)
say “You Win!”
else
say “Try Again!”
- The same logic in Python:
if score > 50:
print(“You Win!”)
else:
print(“Try Again!”)
While Scratch uses a drag-and-drop block interface, Python relies on precise syntax, including indentation.
Example of a Loop in Scratch
- A repeat block in Scratch:
repeat 5
move 10 steps
- The equivalent for loop in Python:
for i in range(5):
move(10) # Move 10 steps, 5 times
Although Scratch provides a visual representation, Python enables more powerful and customizable program control.
4. Key Takeaways
- Conditional statements (if, elif, else) allow programs to make decisions.
- Loops (for, while) help repeat actions efficiently.
- Indentation matters in Python – unlike Scratch, where block connections define structure, Python uses whitespace and indentation to determine the program’s logic.
- Python and Scratch have the same logic but different ways of expressing it.
- Learning control structures in Python is crucial for creating interactive programs and will be foundational for advanced programming in later years.
By mastering control structures, students will be ready to write more complex, efficient, and interactive programs in Python!