Solution development: algorithmic thinking and introductory programming – Week 5 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: 5
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.
This week, we delve deeper into algorithmic thinking and introductory programming, building upon the foundational concepts we established in previous weeks. We are now moving beyond simple sequences and exploring more complex control structures, specifically focusing on selection (if-else statements). This is crucial because real-world problems rarely follow a straight, linear path. They often involve making choices based on different conditions. Learning to implement these conditional statements will enable you to write programs that can handle diverse scenarios and make decisions, just like humans do.
What is Selection (Conditional Statements)? Selection, also known as conditional execution, allows a program to execute different blocks of code based on whether a certain condition is true or false. Think of it as the program making a decision. The most common conditional statement is the `if` statement. We can extend this with `else` to provide an alternative action if the condition is false, and `elif` to check multiple conditions sequentially. The `if` Statement: The `if` statement evaluates a Boolean expression (an expression that results in either `True` or `False`). If the expression is `True`, the code block indented below the `if` statement is executed. If it's `False`, the code block is skipped.
Syntax (Python): ```python if condition: Code to execute if the condition is True statement1 statement2 ... ```
Example: ```python age = 16 if age >= 18: print("You are eligible to vote.") ``` In this example, the condition `age >= 18` is evaluated. Since `age` is 16, the condition is `False`, and the `print` statement is skipped. The `else` Statement: The `else` statement provides an alternative code block to execute if the `if` condition is `False`.
Syntax (Python): ```python if condition: Code to execute if the condition is True statement1 else: Code to execute if the condition is False statement2 ```
Example: ```python age = 16 if age >= 18: print("You are eligible to vote.") else: print("You are not yet eligible to vote.") ``` Here, since `age` is 16, the `if` condition is `False`, so the `else` block is executed, printing "You are not yet eligible to vote." The `elif` Statement: The `elif` (short for "else if") statement allows you to check multiple conditions sequentially. It is placed between the `if` and `else` statements. The `elif` condition is only checked if the preceding `if` (or `elif`) condition was `False`.
Syntax (Python): ```python if condition1: Code to execute if condition1 is True statement1 elif condition2: Code to execute if condition1 is False and condition2 is True statement2 else: Code to execute if all preceding conditions are False statement3 ```
Example: Imagine a program to determine the price of a taxi ride based on the distance travelled. Let's say the pricing structure is as follows: 0-5 km: R30 6-10 km: R50 More than 10 km: R80 ```python distance = 7 # Example distance if distance = 5000 and income 500: discount = 0.10 * purchase_amount final_price = purchase_amount - discount print("Discount: R", discount) print("Final price: R", final_price) else: print("No discount applied.") print("Final price: R", purchase_amount) ```
Commentary: This program calculates the discount only if the `purchase_amount` exceeds R
5
0
0. We calculate the discount amount and subtract it from the original price to get the `final_price`. The `float(input())` is used for handling decimal values for the purchase amount.
Question 3: Write a program to determine the grade based on a student's percentage score, according to the following grading system: 90-100: A 80-89: B 70-79: C 60-69: D Below 60: Fail Solution: ```python score = float(input("Enter your percentage score: ")) if 90 <= score <= 100: grade = "A" elif 80 <= score <= 89: grade = "B" elif 70 <= score <= 79: grade = "C" elif 60 <= score <= 69: grade = "D" else: grade = "Fail" print("Your grade is:", grade) ```
Commentary: This program uses `elif` statements to check multiple score ranges. The chained comparisons ensure that each range is checked correctly. The `else` statement handles the case where the score is below
6
0. Independent Practice (Questions Only) Write a program that takes three numbers as input and prints the largest number. Write a program that checks if a given year is a leap year. (A leap year is divisible by 4, but not divisible by 100 unless it is also divisible by 400). Write a program that simulates a simple ATM. Allow the user to enter their account balance and then withdraw money. If the withdrawal amount exceeds the balance, display an error message. Otherwise, update the balance and display the new balance. A cellular service provider offers different data packages: Package A: 1GB for R50 Package B: 3GB for R120 Package C: 5GB for R200 Write a program that prompts the user to enter the desired package and then displays the data allowance and price. If the user enters an invalid package, display an error message. Write a program that determines if a triangle is equilateral, isosceles, or scalene based on the lengths of its three sides. (Equilateral: all sides are equal, Isosceles: two sides are equal, Scalene: no sides are equal). Write a program that simulates a simple grade calculator. Take marks for three subjects from the user and print the average mark. Then, assign a grade based on the average mark, using the same grading system as in Guided Practice Question
3. Develop a program for a small business that calculates employee bonuses.