Welcome to Algorithmic Fundamentals!
Hello there! Welcome to the very first step of your H2 Computing journey. In this chapter, we are going to explore the "brain" of programming: Algorithms. Think of an algorithm as a simple recipe. Just as a recipe tells you how to turn eggs and flour into a cake, an algorithm tells a computer how to turn raw data into a useful result. Don't worry if this seems a bit abstract at first—by the end of these notes, you'll see that you already use algorithms every single day!
1. The Three Pillars of Logic: Sequence, Selection, and Iteration
To control how a program runs, we use three basic "building blocks." You can combine these to create any program imaginable!
A. Sequence: The "Step-by-Step"
Sequence is the simplest construct. It means the computer follows instructions one after another, from top to bottom. It never skips a line unless we tell it to.
Example: Getting ready for school.
1. Wake up.
2. Brush teeth.
3. Put on uniform.
If you change the order (like putting on your uniform before showering!), things might go wrong. Order matters!
B. Selection: The "Decision Maker"
Selection allows the program to choose different paths based on a condition. We usually use IF, THEN, and ELSE for this.
Analogy: The Umbrella Choice.
IF it is raining, THEN take an umbrella. ELSE (if it's not raining), take sunglasses.
This makes our programs "smart" because they can react to different situations.
C. Iteration: The "Looper"
Iteration means repeating a set of instructions. Computers are great at this because they never get tired or bored!
Example: Eating cereal.
While there is still cereal in the bowl, keep taking a spoonful. Once the bowl is empty, you stop.
Memory Aid: Think of a "treadmill." You keep running (iterating) until the timer hits zero or you press stop (the condition).
Quick Review Box:
• Sequence: Instructions in order.
• Selection: Making a choice (IF).
• Iteration: Repeating (Loops).
2. Understanding and Devising Pseudocode
When we design algorithms, we often use Pseudocode. The word "pseudo" means "fake," so this is literally "fake code."
Why do we use it? Because real programming languages (like Python) are very picky about brackets and commas. Pseudocode lets us focus on the logic without worrying about "syntax errors."
How to write good Pseudocode:
1. Use INPUT to get data from the user.
2. Use OUTPUT or PRINT to show results.
3. Use STORE or the arrow \( \leftarrow \) to save values into variables.
4. Use clear names like total_score instead of just x.
Example: An algorithm to see if you passed a test.
INPUT mark
IF mark >= 50 THEN
OUTPUT "You passed!"
ELSE
OUTPUT "Try again!"
ENDIF
Did you know? There is no "official" rulebook for pseudocode in the H2 syllabus! As long as your logic is clear and consistent, you will get the marks. However, staying close to Python-like logic is usually a safe bet.
Key Takeaway: Pseudocode is a bridge between a human idea and a computer program. It is meant to be read by humans, not machines!
3. The Modular Approach: Decomposition
Sometimes, a problem is just too big to handle all at once. Imagine being told to "Build a Car." That's overwhelming! This is where Decomposition comes in.
Decomposition is the process of breaking a complex problem down into smaller, more manageable parts (modules).
Real-world Example: Planning a School Carnival.
Instead of one person doing everything, you break it down:
• Module 1: Food and Drinks stall.
• Module 2: Games and Activities.
• Module 3: Ticket sales.
• Module 4: Marketing.
Why use the Modular Approach?
• Easier to manage: Small parts are easier to understand than one giant block of code.
• Teamwork: Different people can work on different modules at the same time.
• Easier Testing: You can test if the "Food Stall" works perfectly before moving on to "Games."
Common Mistake: Students often try to write the whole program in one go. If it fails, they don't know where the error is. Always decompose first!
4. The Incremental Approach
The Incremental Approach is slightly different from decomposition. It involves solving a "mini version" of the problem first, then gradually adding more features until the full problem is solved.
Analogy: Drawing a portrait.
First, you draw a rough circle for the head (the basic version). Then you add the eyes and nose (extending the solution). Finally, you add shading and hair (the complete version).
Step-by-Step Process:
1. The Core: Solve the most basic requirement first. (e.g., A calculator that only adds).
2. Expand: Add the next feature. (e.g., Now it can subtract).
3. Repeat: Keep adding until it meets all requirements.
Encouraging Phrase: Don't worry if your first version of a program is very simple. Even the world's most complex apps started as a tiny "Version 1.0"!
Quick Comparison:
• Decomposition: Breaking a big thing into many small pieces.
• Incremental: Starting with a small thing and making it bigger.
Chapter Summary - Key Takeaways
• Control Flow: Use Sequence (order), Selection (IF), and Iteration (Loops) to tell the computer what to do and when.
• Pseudocode: A logical way to plan your code without worrying about strict language rules.
• Decomposition: Tackle big problems by breaking them into smaller "modules."
• Incremental Approach: Start with a simple solution and build on it step-by-step.
Congratulations! You've just mastered the foundations of algorithmic thinking. You're now ready to start turning your ideas into actual logic!