Solution development: structured programming and control structures – Week 2 focus
Download the Lessonotes Mobile South Africa app for faster lesson access on Android and iPhone.
Subject: Information Technology
Class: Grade 11
Term: 1st Term
Week: 2
Theme: General lesson support
This page supports the lesson note with a companion video and a short classroom-ready summary.
For class groups and homework, share this lesson page so learners also get the summary, objectives, and full lesson context.
Structured programming is a fundamental paradigm in computer science that helps us develop clear, efficient, and maintainable programs. It's especially crucial in a country like South Africa, where software solutions are increasingly needed to address diverse challenges, from improving healthcare access to streamlining business operations. Well-structured code leads to fewer errors, easier collaboration among developers, and ultimately, more reliable systems that benefit our communities. This week, we'll focus on control structures, the building blocks that dictate the flow of execution in a program. We will be looking at sequence, selection (if-then-else), and iteration (loops).
Structured Programming: This is a programming paradigm aimed at improving code clarity, quality, and development time by using only three basic control structures: Sequence: Instructions are executed in the order they appear in the code. This is the default flow of execution.
Selection: Allows the program to choose between different paths of execution based on a condition. Implemented using `if`, `if-else`, and `if-elif-else` statements.
Iteration: Allows a block of code to be executed repeatedly based on a condition. Implemented using `for` and `while` loops.
Sequence in Detail: This is the most straightforward control structure. Code statements are executed line by line, from top to bottom.
Example: ```python
Example: Calculating the area of a rectangle length = 10 width = 5 area = length * width print("The area of the rectangle is:", area) ``` Explanation: The program first assigns values to `length` and `width`. Then, it calculates the `area` and finally prints the result. Each line is executed in order. Selection (if, if-else, if-elif-else) in Detail: Selection structures allow a program to make decisions. `if` statement: Executes a block of code only if a condition is true. ```python
Example: Checking if a student passed mark = 55 if mark >= 50: print("Congratulations! You passed.") ``` Explanation: The code checks if the student's `mark` is greater than or equal to
5
0. If it is, the message "Congratulations! You passed." is printed. `if-else` statement: Executes one block of code if a condition is true and another block of code if the condition is false. ```python
Example: Checking if a number is even or odd number = 7 if number % 2 == 0: print("The number is even.") else: print("The number is odd.") ``` Explanation: The code checks if the `number` is divisible by
2. If the remainder is 0, it's even; otherwise, it's odd. `if-elif-else` statement: Allows for multiple conditions to be checked in sequence. `elif` stands for "else if". ```python
Example: Grading a student based on their mark mark = 75 if mark >= 80: grade = "A" elif mark >= 70: grade = "B" elif mark >= 60: grade = "C" elif mark >= 50: grade = "D" else: grade = "F" print("The student's grade is:", grade) ``` Explanation: This code checks the `mark` against different ranges. The first condition that evaluates to true determines the student's `grade`. If none of the conditions are true, the `else` block is executed, assigning a grade of "F". Iteration (for, while) in Detail: Iteration structures allow a block of code to be executed repeatedly. `for` loop: Used when you know in advance how many times you want to execute a block of code. It's often used to iterate over a sequence (e.g., a list or range). ```python
Example: Printing numbers from 1 to 5 for i in range(1, 6): # range(start, end+1) print(i) ``` Explanation: The `for` loop iterates through the numbers generated by the `range(1, 6)` function, which creates a sequence of numbers from 1 to
5. For each number in the sequence, the `print(i)` statement is executed. `while` loop: Used when you want to execute a block of code repeatedly as long as a condition is true. ```python
Example: Counting down from 10 to 1 count = 10 while count >= 1: print(count) count = count - 1 ``` Explanation: The `while` loop continues to execute as long as the `count` variable is greater than or equal to
1. Inside the loop, the current value of `count` is printed, and then `count` is decremented by
1. It is crucial to ensure the condition eventually becomes false to avoid an infinite loop.
Nested Control Structures: Control structures can be nested inside each other. This allows for more complex program logic. For example, an `if` statement can be placed inside a `for` loop or a `while` loop. ```python
Example: Finding prime numbers between 1 and 20 for number in range(2, 21): is_prime = True for i in range(2, number): if number % i == 0: is_prime = False break # exit inner loop when not prime if is_prime: print(number, "is a prime number") ``` Explanation: The outer `for` loop iterates through numbers from 2 to
2
0. The inner `for` loop checks if each number is divisible by any number between 2 and itself. If it is divisible, it's not a prime number, and the `is_prime` flag is set to `False`. The `break` statement exits the inner loop immediately when a divisor is found. The `if is_prime:` statement checks if the number is still considered prime after the inner loop completes, and if so, it prints the number. Guided Practice (With Solutions)
Question 1: Write a program that prompts the user to enter their age. If the age is less than 18, print "You are not eligible to vote." Otherwise, print "You are eligible to vote." Solution: ```python age = int(input("Enter your age: ")) if age 0: print("The number is positive.") elif number < 0: print("The number is negative.") else: print("The number is zero.") ```
Commentary: This question demonstrates the `if-elif-else` statement.