Control Structure SS2 Digital Technologies Lesson Note

Download Lesson Note
Lesson Notes

Topic: Control Structure

If a program just ran from the top line to the bottom line without stopping, it wouldn’t 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.

 

The IF-ELSE Statement: The Decision Maker

This is the most common tool in programming. It works exactly like a real-life choice.

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

In Python Code:

Python

score = 65

 

if score >= 50:

    print(“You Passed!”)

else:

    print(“Try again next time.”)

 

Key Points:

  • The Condition: This is the question being asked (e.g., score >= 50).
  • The Indentation: Notice the gap (space) before the print. This tells the computer that the action “belongs” to that choice.

 

Loops: The “Don’t Repeat Yourself” Tool

Imagine I asked you to write “I will not talk in class” 100 times in your notebook. You’d be tired! A computer doesn’t get tired. A Loop tells the computer to repeat a block of code as long as a certain condition is met.

  1. The “For” Loop (The Counter)

Used when you know exactly how many times you want to repeat something.

  • Example: “For every student in this class, give them a textbook.”
  1. The “While” Loop (The Watchman)

Used when you want to repeat something until a specific event happens.

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

 

Functions: The “Reusable Recipe”

A Function is a saved block of code that performs a specific task. 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/Parameters).
  3. Process: The blender spins (The Code).
  4. Output: You get juice (The Return Value).

Example:

Python

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”)

 

Summary Table for Your Project

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.

 

Common Mistakes to Avoid

  • Forgetting the Colon (:): In Python, every if, else, for, and def must end with a :. It’s like a full stop at the end of a sentence.
  • Bad Indentation: If your code isn’t lined up correctly, the computer won’t know which code belongs to which loop.
  • The Infinite Loop: If you use a while loop but forget to change the condition (e.g., while 1 < 2), the computer will run forever until it crashes!

 

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.

Lesson Notes for Other Classes