Lesson Notes By Weeks and Term v5 - Grade 11

Solution development: structured programming and control structures – Week 5 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: 5

Theme: General lesson support

Lesson Video

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.

Performance objectives

Lesson summary

This week, we delve into the fundamental principles of structured programming and control structures. These concepts are the bedrock of all software development, enabling us to create programs that solve complex problems in a clear, organized, and predictable manner. Imagine you want to automate a process, like calculating the fares for a taxi service based on distance and time, or perhaps design a program to manage a school's library inventory. Structured programming provides the tools and methodology to break down these tasks into manageable, logical steps.

Lesson notes

2.1 Structured Programming: Structured programming is a programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making extensive use of subroutines, block structures, for and while loops, and if-then-else blocks. It emphasizes modularity, readability, and maintainability.

The key principles are: Top-Down Design: Breaking down a complex problem into smaller, more manageable sub-problems. This "divide and conquer" approach simplifies the development process.

Modularity: Dividing the program into independent modules or functions, each performing a specific task. This makes the code easier to understand, debug, and reuse. Single Entry, Single Exit: Each control structure (like a loop or an if statement) should have only one entry point (where it starts) and one exit point (where it ends). This makes the flow of control easier to follow. Avoidance of `goto` Statements: `goto` statements can create spaghetti code, making the program difficult to understand and debug. Structured programming advocates for using control structures instead. 2.2 Control Structures: Control structures determine the order in which instructions are executed in a program.

There are three fundamental types: Sequence: Instructions are executed one after another, in the order they appear in the code.

Example:* Calculating the area of a rectangle. ```python length = 10 # Input length in meters width = 5 # Input width in meters area = length * width # Calculate area in square meters print("The area of the rectangle is:", area, "square meters") # Display the area ``` This code follows a sequence: first, the length and width are assigned values, then the area is calculated, and finally, the result is printed.

Selection (Conditional Statements): Allows the program to make decisions based on a condition. The most common selection structures are `if`, `if-else`, and `if-elif-else` statements. `if` statement: Executes a block of code only if a condition is true.

Example:* Checking if a student passed an exam. ```python mark = 55 # Student's mark passing_mark = 50 # Passing mark if mark >= passing_mark: print("Congratulations! You passed the exam.") # Display if the student passed ``` `if-else` statement: Executes one block of code if a condition is true and another block if the condition is false.

Example:* Determining if a number is even or odd. ```python number = 7 # Input number if number % 2 == 0: # Check if the number is divisible by 2 (even) print(number, "is an even number.") # Display if even else: print(number, "is an odd number.") # Display if odd ``` `if-elif-else` statement: Allows you to check multiple conditions in sequence.

Example:* Assigning a grade based on a percentage. ```python percentage = 75 # Student's percentage if percentage >= 80: # Check if the percentage is 80 or above grade = "A" # Assign A elif percentage >= 70: # Check if the percentage is 70 or above grade = "B" # Assign B elif percentage >= 60: # Check if the percentage is 60 or above grade = "C" # Assign C elif percentage >= 50: # Check if the percentage is 50 or above grade = "D" # Assign D else: grade = "F" # Assign F print("Your grade is:", grade) # Display grade ``` Iteration (Loops): Allows you to repeat a block of code multiple times. The most common iteration structures are `for` and `while` loops. `for` loop: Repeats a block of code a specific number of times, often used to iterate over a sequence (like a list or a string).

Example:* Printing the numbers from 1 to 5. ```python for i in range(1, 6): # Iterate from 1 to 5 (exclusive of 6) print(i) # Display the value of i ``` `while` loop: Repeats a block of code as long as a condition is true.

Example:* Calculating the sum of numbers until the sum exceeds 100. ```python sum = 0 # Initialize sum number = 1 # Initialize number while sum largest: # Check if current number is greater than largest largest = numbers[i] # Update largest index = i # Update index print("The largest number is:", largest) # Display largest print("Its index is:", index) # Display index ``` Guided Practice (With Solutions)

Question 1: Write a program that takes a student's score as input and prints "Pass" if the score is 50 or above, and "Fail" otherwise.

Solution: ```python score = int(input("Enter the student's score: ")) # Get the score if score >= 50: # Check if the score is greater than or equal to 50 print("Pass") # Display "Pass" else: print("Fail") # Display "Fail" ```

Commentary: This problem uses a simple `if-else` statement to implement a decision based on the student's score. The `input()` function is used to get the score from the user. The `int()` function converts the input (which is initially a string) to an integer.

Question 2: Write a program that prints the numbers from 1 to 10 using a `while` loop.