Lesson Notes By Weeks and Term v5 - Grade 10

Solution development: algorithmic thinking and introductory programming – Week 4 focus

Download the Lessonotes Mobile South Africa app for faster lesson access on Android and iPhone.

Subject: Information Technology

Class: Grade 10

Term: 2nd 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 world of algorithmic thinking and introductory programming. Building upon the foundation laid in previous weeks, we will focus on control structures, specifically conditional statements (if, else if, else) and looping structures (for loops, while loops). These structures are the building blocks of virtually every program and are essential for creating solutions to real-world problems. Imagine designing a traffic light system, controlling a robotic arm in a factory, or even creating a simple game – all of these require the use of conditional and looping structures.

Lesson notes

Conditional Statements (`if`, `elif`, `else`) Conditional statements allow our programs to make decisions. The `if` statement evaluates a condition, and if the condition is true, it executes a block of code. The `elif` (else if) statement allows us to check multiple conditions, and the `else` statement provides a default action if none of the preceding conditions are true.

Syntax: ```python if condition: Code to execute if the condition is true elif another_condition: Code to execute if another_condition is true else: Code to execute if none of the above conditions are true ``` Explanation: `if`: The `if` keyword initiates a conditional block. The `condition` is a Boolean expression (evaluating to `True` or `False`). The code indented below the `if` statement is executed only if the condition is `True`. `elif`: `elif` (short for "else if") allows you to check multiple conditions sequentially. If the `if` condition is `False`, the `elif` condition is evaluated. You can have multiple `elif` statements. `else`: The `else` block is optional. It provides a block of code to be executed if none of the `if` or `elif` conditions are `True`.

Example 1: Determining Pass/Fail based on a student's mark. Let's say we want to determine if a student has passed or failed based on their mark. A passing mark is 50 or higher. ```python mark = 65 if mark >= 50: print("Pass") else: print("Fail") ``` In this example, the `mark` variable holds the student's mark. The `if` statement checks if the mark is greater than or equal to

5

0. If it is, the program prints "Pass"; otherwise, it prints "Fail".

Example 2: Assigning grades based on percentage. We can use `elif` statements to assign grades based on a percentage: ```python percentage = 78 if percentage >= 80: grade = "A" elif percentage >= 70: grade = "B" elif percentage >= 60: grade = "C" elif percentage >= 50: grade = "D" else: grade = "F" print("Grade:", grade) ``` In this example, we use multiple `elif` statements to check different ranges of percentages and assign the corresponding grade. If none of the `if` or `elif` conditions are met, the `else` block assigns an "F" grade. Looping Structures (`for` loops, `while` loops) Looping structures allow us to repeat a block of code multiple times. `for` loops are typically used when we know in advance how many times we want to repeat the code, while `while` loops are used when we want to repeat the code until a certain condition is met. `for` loops The `for` loop iterates over a sequence (e.g., a list, a string, a range of numbers).

Syntax: ```python for variable in sequence: Code to execute for each item in the sequence ``` Explanation: `for`: The `for` keyword initiates a loop. `variable`: This is a variable that takes on the value of each item in the `sequence` during each iteration of the loop. `sequence`: This is an iterable object (e.g., a list, a tuple, a string, a range) that provides the values to be iterated over.

Example 3: Printing numbers from 1 to

5. We can use a `for` loop with the `range()` function to print numbers from 1 to 5. ```python for i in range(1, 6): #range(start, end) the loop will run from start up to but not including end. print(i) ``` The `range(1, 6)` function generates a sequence of numbers from 1 to 5 (not including 6). The `for` loop iterates over this sequence, assigning each number to the `i` variable and printing it.

Example 4: Iterating through a list of provinces. ```python provinces = ["Gauteng", "Western Cape", "KwaZulu-Natal", "Limpopo"] for province in provinces: print("The province is:", province) ``` This loop iterates through each element of the `provinces` list, printing the name of each province. `while` loops The `while` loop repeats a block of code as long as a certain condition is true.

Syntax: ```python while condition: Code to execute while the condition is true ``` Explanation: `while`: The `while` keyword initiates a loop. `condition`: This is a Boolean expression that is evaluated before each iteration of the loop. The code inside the loop is executed only if the condition is `True`.

Important: You must ensure that the condition eventually becomes `False` within the loop to prevent an infinite loop.

Example 5: Printing numbers from 1 to 5 using a `while` loop. ```python i = 1 while i secret_number: print("Too high!") print("Congratulations! You guessed the number.") ``` This example shows a while loop used for a game. The loop continues until the user guesses the `secret_number`.