Welcome to the Building Blocks of Programs!
Hi there! Today, we are going to explore the three most important tools in a programmer's toolbox: Constructs. Think of these as the "Lego bricks" of code. No matter how complicated an app or a game looks, it is actually just made up of these three basic patterns.
In this chapter, we will learn how to read these patterns in flowcharts and how to use them in Python. Don't worry if this seems a bit like a puzzle at first—once you see the patterns, you'll be able to "read" any program like a book!
1. The Three Main Constructs
Every algorithm you write uses one or more of these three constructs:
1. Sequence: Doing things in a specific order, one after another.
2. Selection: Making a choice (a "fork in the road").
3. Iteration: Repeating steps over and over (a "loop").
Real-World Analogy:
Imagine you are making a cup of Milo:
- Sequence: First put the powder in, then add hot water, then stir.
- Selection: IF you want it sweet, THEN add condensed milk. ELSE, leave it plain.
- Iteration: Stir the Milo WHILE there are still clumps of powder visible.
2. Sequence: The Step-by-Step Path
Sequence is the simplest construct. It means the computer follows your instructions exactly in the order you wrote them, from top to bottom.
In a Flowchart:
You will see boxes connected by arrows pointing straight down. Each box happens only after the one above it is finished.
In Python:
Each line of code is executed one by one.
Example:
print("Hello!")
print("How are you?")
print("Goodbye!")
Quick Review: In a Sequence, the order is everything! If you put your socks on after your shoes, it doesn't work. The same applies to code.
3. Selection: The Decision Maker
Selection is used when the program needs to make a decision based on a condition (a question that is either True or False).
In a Flowchart:
Selection is represented by a Diamond Shape. It usually has two arrows coming out of it: one labeled "Yes" (True) and one labeled "No" (False).
In Python:
We use the keywords if, elif (short for "else if"), and else.
Type 1: The Simple If
Used if you only want to do something when a condition is true.
if score > 50:
print("You passed!")
Type 2: If-Else
Used when you have two clear choices.
if temperature > 30:
print("It is hot!")
else:
print("It is cool!")
Type 3: Multi-way Selection (If-Elif-Else)
Used when you have many different options.
if grade == "A":
print("Excellent!")
elif grade == "B":
print("Good job!")
else:
print("Keep trying!")
Common Mistake to Avoid: Don't forget the colon (:) at the end of your if and else lines in Python! Also, remember that code inside the selection must be indented (pushed to the right).
Key Takeaway: Selection allows your program to be "smart" and react differently to different inputs.
4. Iteration: The Power of Repetition
Iteration (or "looping") is when the computer repeats a block of code. This is what computers are best at—they never get tired of doing the same thing!
In a Flowchart:
You will see an arrow that loops back up to an earlier part of the flowchart. This creates a circle or a "loop".
In Python:
There are two main types of loops you need to know:
1. The "While" Loop (Condition-controlled)
This repeats as long as a condition is True. We use it when we don't know exactly how many times we need to repeat.
Example: Keep asking for a password while the input is wrong.
2. The "For" Loop (Count-controlled)
This repeats a specific number of times. We use it when we know exactly how many laps to run.
Example: Print "Hello" 10 times.
for i in range(10):
print("Hello")
Memory Aid: Think of a While loop like "While I am hungry, I will eat." Think of a For loop like "For 3 minutes, I will brush my teeth."
Did you know? An Infinite Loop happens when the condition never becomes False. The computer will keep running that code forever until you force it to stop!
5. Summary Table for Flowcharts
When you are interpreting flowcharts for the O-Level exam, keep this quick guide in mind:
Oval (Start/Stop): Where the program begins and ends.
Rectangle (Process): A simple action or calculation (Sequence).
Parallelogram (Input/Output): Getting data from a user or printing to a screen.
Diamond (Decision): A question that leads to a choice (Selection or the start of a Loop).
Arrow pointing backwards: Indication of a loop (Iteration).
Quick Review Quiz!
Can you identify which construct is being used in these scenarios?
1. A calculator adds two numbers and shows the result. (Answer: Sequence)
2. A game checks if your health is \( 0 \) to see if the game is over. (Answer: Selection)
3. An app sends a "Happy Birthday" email to every person in a contact list. (Answer: Iteration)
Final Encouragement: You've just covered the foundation of all programming! If you can master these three constructs—Sequence, Selection, and Iteration—you can build almost anything. Keep practicing drawing flowcharts and writing small Python snippets, and it will become second nature!