Lesson Notes By Weeks and Term v5 - Grade 11

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

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 deeper into the fundamental concepts of structured programming and control structures. These concepts are the building blocks for creating logical and efficient programs. Mastering these skills is crucial for developing effective solutions to computational problems. In the South African context, understanding structured programming can empower you to develop innovative solutions for challenges ranging from optimizing farming practices to improving access to education and healthcare through technology.

Lesson notes

Structured programming is a programming paradigm that emphasizes the use of control structures to create well-organized and easy-to-understand code. It avoids the use of "goto" statements, which can lead to spaghetti code.

The three main control structures are: Sequence: Statements are executed in the order they appear in the code. This is the default control flow. Think of it as following a recipe step-by-step.

Example (Python): ```python Calculate the area of a rectangle length = 10 width = 5 area = length * width print("The area of the rectangle is:", area) ``` In this example, each line of code is executed in sequence, one after the other.

Selection (if/else): Allows the program to make decisions based on certain conditions. If a condition is true, a specific block of code is executed; otherwise, a different block of code (or no code) is executed.

Example (Python): ```python Determine if a student passed or failed mark = 60 if mark >= 50: print("Passed") else: print("Failed") ``` Here, the program checks if the `mark` is greater than or equal to

5

0. If it is, "Passed" is printed; otherwise, "Failed" is printed. We could also include `elif` (else if) statements for more complex conditions.

For example: ```python Classifying a student's mark mark = 75 if mark >= 75: print("Distinction") elif mark >= 60: print("Pass") elif mark >= 50: print("Bare Pass") else: print("Fail") ``` This example shows how we can use multiple conditions to classify a student's performance.

Iteration (Loops): Allows a block of code to be executed repeatedly until a certain condition is met.

There are two main types: `while` loop: Executes a block of code as long as a condition is true.

Example (Python): ```python Print numbers from 1 to 5 count = 1 while count = 80: grade = "A" elif mark >= 70: grade = "B" elif mark >= 60: grade = "C" elif mark >= 50: grade = "D" else: grade = "Fail" print("The student's grade is:", grade) ``` Explanation: We first get the student's mark as input and convert it to an integer. We then use a series of `if` and `elif` statements to check the mark against the grading criteria. Based on the mark, the appropriate grade is assigned to the `grade` variable. Finally, we print the student's grade. Guided Practice (With Solutions)

Question 1: Write a program that asks the user to enter two numbers and then prints the larger of the two numbers.

Solution: ```python num1 = float(input("Enter the first number: ")) num2 = float(input("Enter the second number: ")) if num1 > num2: print("The larger number is:", num1) elif num2 > num1: print("The larger number is:", num2) else: print("The numbers are equal.") ```

Commentary: We use an `if/elif/else` structure to compare the two numbers and print the larger one. The `float()` function is used to allow the user to enter decimal numbers. We handle the case where the numbers are equal.

Question 2: Write a program that asks the user to enter a number and then prints whether the number is positive, negative, or zero.

Solution: ```python num = float(input("Enter a number: ")) if num > 0: print("The number is positive.") elif num < 0: print("The number is negative.") else: print("The number is zero.") ```

Commentary: This problem uses a similar `if/elif/else` structure to categorize the input number based on its value.

Question 3: Write a program that uses a `while` loop to print the first 10 even numbers.

Solution: ```python count = 0 number = 2 while count < 10: print(number) number = number + 2 count = count + 1 ```

Commentary: We initialize `count` to 0 and `number` to 2 (the first even number). The `while` loop continues until `count` reaches

1

0. Inside the loop, we print the current value of `number`, increment `number` by 2 to get the next even number, and increment `count` to keep track of how many even numbers we have printed.

Question 4: Write a program that uses a `for` loop to calculate the sum of the numbers from 1 to

1

0

0. Solution: ```python sum = 0 for i in range(1, 101): # range(1,101) generates numbers from 1 to 100 sum = sum + i print("The sum of the numbers from 1 to 100 is:", sum) ```

Commentary: We initialize `sum` to

0. The `for` loop iterates through the numbers from 1 to

1

0

0. In each iteration, we add the current number to the `sum`. Independent Practice (Questions Only) Write a program that asks 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." Write a program that asks the user to enter three numbers. Find and print the largest of the three numbers. Write a program that uses a `while` loop to print the numbers from 10 down to

1. Write a program that uses a `for` loop to print the multiplication table of a number entered by the user (up to 10). Write a program to determine if a number entered by the user is a prime number. Write a program that calculates the factorial of a number entered by the user.