Skip to content

Control Structure – SS2 Digital Technologies Lesson Note (Second Term)

Download this term · ₦1,000

Lesson note at a glance

Subject
Digital Technologies
Class
SS2
Term
Second Term
Topic
Control Structure
Order
Topic 4 of 8 this term
Reading time
4 min

Control structures are the parts of a program that decide which lines of code run and how many times they run. This lesson covers three of them: the if-else statement, which makes decisions, loops, which repeat a block of code, and functions, which save a block of code so you can reuse it. The examples use Python.

Advertisement

Behavioural objectives

By the end of the lesson, students should be able to:

  • explain what a control structure is and why programs need them;
  • write a simple if-else statement in Python;
  • state the difference between a for loop and a while loop;
  • define a function and call it with different inputs;
  • identify common Python mistakes such as a missing colon, bad indentation and an infinite loop.

Why do programs need control structures?

If a program just ran from the top line to the bottom line without stopping, it would not be very useful. Real programs need to make choices.

  • Should I let this user log in? (If-Else)
  • Should I send this email to all 500 students? (Loops)
  • Should I do this math calculation again and again? (Functions)

Control structures are the “traffic wardens” of your code. They tell the computer when to stop, when to turn, and when to go back and do something again.

What is an if-else statement?

The if-else statement is the decision maker. It is the most common tool in programming, and it works like a real-life choice.

The logic: “If I have money, I will buy lunch. Else (otherwise), I will drink water.”

In Python code (the two print lines are indented by four spaces):

score = 65
if score >= 50:
print(“You Passed!”)
else:
print(“Try again next time.”)

Key points

  • The condition: This is the question being asked (for example, score >= 50).
  • The indentation: Notice the gap (space) before the print. This tells the computer that the action “belongs” to that choice.

What are loops?

A loop is the “don’t repeat yourself” tool. Imagine I asked you to write “I will not talk in class” 100 times in your notebook. You would be tired, but a computer does not get tired. A loop tells the computer to repeat a block of code as long as a certain condition is met.

Advertisement

The for loop (the counter)

Use a for loop when you know exactly how many times you want to repeat something.

  • Example: “For every student in this class, give them a textbook.”

The while loop (the watchman)

Use a while loop when you want to repeat something until a specific event happens.

  • Example: “Keep playing the music while the battery is above 10%.”

What is a function?

A function is a saved block of code that performs a specific task. It is a reusable recipe. Instead of writing the same 10 lines of code every time you need to calculate a student’s grade, you write it once, give it a name, and “call” it whenever you need it.

Think of it like a blender

  1. Define it: You build the blender (the function).
  2. Input: You put in fruit (the data or parameters).
  3. Process: The blender spins (the code).
  4. Output: You get juice (the return value).

Example of a function

The print line is indented by four spaces. To use the function, you just call its name.

def welcome_student(name):
print(“Hello ” + name + “, welcome to the Computer Lab!”)

# To use it, you just call the name:
welcome_student(“Bisi”)
welcome_student(“Obi”)

Which control tool should I use?

Tool Human meaning When to use it
If-Else “Only do this if…” Checking passwords or grades.
For loop “Do this X times.” Printing a list or a timetable.
While loop “Do this until…” Running a game until ‘Game Over’.
Function “Save this task for later.” Calculating tax or cleaning data.

What mistakes should I avoid?

  • Forgetting the colon (:): In Python, every if, else, for and def line must end with a colon. It is like a full stop at the end of a sentence.
  • Bad indentation: If your code is not lined up correctly, the computer will not know which code belongs to which loop.
  • The infinite loop: If you use a while loop but forget to change the condition (for example, while 1 < 2), the computer will run forever until it crashes.
Advertisement

Class lab activity

  1. The Bouncer: Write a program that asks for a user’s age. If they are under 13, tell them “You are too young for Facebook.” Else, tell them “Welcome!”
  2. The Multiplier: Use a for loop to print the 5 times table (5, 10, 15… up to 60).
  3. The Shortcut: Create a function called calculate_area that takes the length and width of a room and tells you the size.

Evaluation

  1. What is a control structure?
  2. What is the job of the condition in an if-else statement?
  3. State one difference between a for loop and a while loop.
  4. What is a function, and why is it useful?
  5. Name two mistakes that can stop a Python program from working correctly.

Assignment

In your notebook, explain the blender comparison for a function in your own words. Then give one example of when you would use each of these: an if-else statement, a for loop and a while loop.

This term's Digital Technologies topics

SS2 Second Term · 8 topics

  1. Computer Programming I
  2. Computer Programming II
  3. Programming Basics
  4. Control Structure
  5. Data Science Basics
  6. Big Data and Analytics
  7. Artificial Intelligence
  8. Machine Learning Basics
Downloads

Get Digital Technologies as PDF and Word

Every week of SS2 Digital Technologies, Second Term, formatted and ready to print. Ad-free, and the copy lock comes off.

Get this term Card, bank transfer, USSD or OPay

Advertisement

Lesson Notes for Other Classes

Print it or edit it in Word

Every note is free to read online. A PDF or Word copy of one subject costs ₦1,000 for one term, or ₦2,000 for all three terms.

Continue with GoogleContinue with Facebook

Sign in first, then choose your class and subject at checkout.