Grade 9
Course ContentProgramming logic (Visual/Block)
Habari Coder! Let's Learn the Secret Language of Computers.
Karibu! Welcome to the exciting world of programming logic. You might think coding is about typing confusing words on a black screen, but what if I told you it’s more like building with LEGOs or giving directions to a friend? At its heart, programming is just about giving instructions. Today, we'll learn how to give these instructions in a clear, logical way using visual blocks – no complicated typing needed! Think of it as telling a story, step-by-step, that a computer can understand.
Imagine explaining to your friend how to make a cup of Stima tea. You would say: "First, boil the water. Second, add the tea leaves. Third, add milk. Finally, add sugar if you want." You have just used programming logic! The order of the steps (the sequence) is very important. You can't add milk before you boil the water, right?
The Three Magic Ingredients of Programming Logic
Every program, from M-Pesa to your favourite video game, is built using three basic ideas. Once you understand these, you can build anything! They are:
- Sequence: The order of instructions, one after the other.
- Selection: Making a choice based on a condition (an "if...then..." situation).
- Iteration: Repeating an instruction over and over again (a loop).
1. Sequence: The Straight Path
Sequence is the most straightforward concept. It's simply doing things in the correct order. Just like when a matatu follows its route from Ngong to the CBD, each step must follow the previous one logically.
Let's visualize a simple sequence for a character in a game to walk to a tree and say "Jambo!".
START
|
V
[ Move 10 steps forward ]
|
V
[ Turn right 90 degrees ]
|
V
[ Move 20 steps forward ]
|
V
[ Say "Jambo!" for 2 secs ]
|
V
END
In block programming, this would look like snapping four blocks together in a single column. The computer reads them from top to bottom.
Image Suggestion: A vibrant, colourful screenshot from Scratch or Blockly showing four blocks snapped together vertically. The first block is a blue 'move' block, the second a blue 'turn' block, the third another 'move' block, and the last is a purple 'say' block.
2. Selection: The Fork in the Road
Life is full of choices, and so is programming! Selection is how we make our programs "smart" by allowing them to make decisions. We use the logic of IF...THEN...ELSE.
Think about this: IF your Safaricom data balance is low, THEN you buy a new bundle, ELSE you continue browsing Instagram. That's a selection!
Let's see how a program can check if a student has passed an exam. The pass mark is 50.
// Let's check a student's score
Student_Score = 65
Pass_Mark = 50
// The Logic:
IF Student_Score is greater than or equal to Pass_Mark THEN
Display "Congratulations! You have passed."
ELSE
Display "Keep trying! You can do it."
// Result for this student: "Congratulations! You have passed."
This decision point can be shown with a flowchart diagram:
+--------------------------+
| Is Student_Score >= 50 ? |
+--------------------------+
/ \
/ \
(YES) (NO)
/ \
/ \
+--------------------------+ +---------------------------+
| Display "You passed!" | | Display "Keep trying!" |
+--------------------------+ +---------------------------+
3. Iteration: The Roundabout (Loops)
Imagine you are a farmer planting 100 maize seeds on your shamba. You wouldn't write down the instruction "Dig hole, plant seed, cover hole" 100 times! That would be exhausting. Instead, you would just say: "Repeat the following action 100 times: Dig, Plant, Cover."
This is called iteration or a loop. It saves time and makes your code short and powerful.
// Simple Loop Logic
REPEAT 10 TIMES:
Clap your hands
END REPEAT
In block programming, you'd use a special C-shaped "repeat" block and place the blocks you want to repeat inside it. It's like putting your actions inside a magic roundabout that goes around for a specific number of times.
+-----------------------------+
| START LOOP (Repeat 4 times)
| |
| +------------------+ |
+---->| Move 10 steps |----+
+------------------+ ^
| |
V |
+------------------+ |
| Turn 90 deg |----+
+------------------+
// This code will make a character walk in a perfect square!
Image Suggestion: An animated GIF of a character on a Scratch stage walking in a square. Beside the stage, show the code blocks: a yellow 'start flag' block connected to a large orange 'repeat 4' C-shaped block. Inside the repeat block, there is a blue 'move 100 steps' block and a blue 'turn 90 degrees' block.
Let's Build Something! A Mini-Project
Let's combine all three concepts to create a simple game: "The Hungry Giraffe".
The Goal: A giraffe moves across the screen. IF it touches an acacia tree, it "eats" (the tree disappears) and the player's score goes up by 1. We want to repeat this until the score is 5.
- Sequence: The giraffe must first move, THEN check if it's touching a tree.
- Selection: IF the giraffe is touching the tree, THEN add 1 to the score and hide the tree.
- Iteration: REPEAT this whole process UNTIL the score is 5.
You have just designed the logic for a game! Using visual blocks, you would drag and snap these ideas together to bring your giraffe to life. You are no longer just a computer user; you are becoming a creator!
Conclusion: You Are a Programmer!
See? Programming logic isn't scary at all. It's just a way of thinking clearly and giving instructions step-by-step. By mastering Sequence, Selection, and Iteration, you have learned the fundamental building blocks of all software. Now, go ahead and open a free tool like Scratch and start building. Create an animation, a story, or a simple game. The power to create is now in your hands. Kazi kwako!
Habari Mwanafunzi! Welcome to the World of Programming Logic!
Ever wondered how a game on your phone knows what to do when you tap the screen? Or how M-Pesa can figure out if you have enough money for a transaction? It's not magic, it's logic! Today, we are going to learn how to give instructions to a computer, not with complicated text, but with colourful blocks, almost like building with LEGOs. Think of it as teaching a computer to think, step-by-step, just like you would explain a plan to a friend.
What is an Algorithm? The Recipe for Success!
Before we start programming, we need a plan. In computer science, this plan is called an algorithm. An algorithm is simply a list of steps to complete a task. You use algorithms every day!
Real-World Example: The Ugali Algorithm
Making perfect ugali requires a specific set of instructions. You can't just mix flour and water randomly!If you miss a step or do them in the wrong order, you end up with a mess! A computer is the same; it needs clear, ordered instructions.
- Boil water in a sufuria.
- Pour in the maize flour slowly.
- Stir continuously with a mwiko to avoid lumps.
- Cook until it is firm.
- Serve with your favourite stew.
The Three Superpowers of Programming Logic
In block programming, we have three main types of instructions, our "superpowers," that can solve almost any problem. Let's explore them!
Image Suggestion: A vibrant, colourful image of a Kenyan classroom. Diverse students are gathered around computers, looking excited and engaged. On the screens, a block-based coding interface like Scratch is visible, with colourful blocks snapped together to make a simple animation of a dancing Tusker elephant. The style is bright and optimistic.
1. Sequence: One Step After Another
A sequence is the simplest logic. It's a series of actions that happen in a specific order, one after the other. Just like the steps in our ugali algorithm.
- Step 1: Get a sufuria.
- Step 2: Add water.
- Step 3: Place on the jiko.
Here is how a sequence looks in a diagram called a flowchart:
[ START ]
|
V
+------------------+
| Action 1 |
| (e.g., Add water)|
+------------------+
|
V
+------------------+
| Action 2 |
| (e.g., Add flour)|
+------------------+
|
V
+------------------+
| Action 3 |
| (e.g., Stir) |
+------------------+
|
V
[ END ]
2. Selection: Making a Choice (If...Then...Else)
Life is full of choices, and so is programming! Selection allows a program to make a decision based on a condition. It asks a "what if?" question.
Real-World Example: Crossing the Road in Nairobi
You are at a zebra crossing. Your brain uses selection logic:This "IF...THEN...ELSE" structure is the heart of decision-making in code.
- IF the road is clear, THEN you cross.
- ELSE (if the road is not clear), THEN you wait.
Here's the flowchart for selection. The diamond shape represents a decision:
+----------------------+
| Is the road clear? |
+----------+-----------+
|
+--------+--------+
| |
(YES) (NO)
| |
V V
+-----------------+ +-----------------+
| Cross Road | | Wait |
+-----------------+ +-----------------+
| |
+--------+--------+
|
V
[ Continue on your way ]
3. Iteration: Do it Again! (Loops)
Iteration, or looping, is about repeating an action multiple times. Why write the same instruction 10 times when you can just tell the computer to repeat it? This saves time and effort!
Real-World Example: Fetching Water
Imagine your mum asks you to fetch 5 jerrycans of water. You don't need five separate plans. You have one plan that you repeat 5 times. This is a "For Loop" because you know exactly how many times to repeat.
Now, imagine she says, "Weed the shamba UNTIL it is clean." You don't know exactly how long it will take, but you know the condition for stopping. This is a "While Loop."
The flowchart for a loop looks like it goes back on itself:
+-------------------------+
| Start Weeding |
+-----------+-------------+
|
V
+---------------------------+ <--+
| Is the shamba clean yet? | | (Repeat)
+-------------+-------------+ |
| (NO) |
V |
+---------------------------+ |
| Continue Weeding a row |----+
+---------------------------+
| (YES)
V
+-------------------------+
| Stop and Rest |
+-------------------------+
Let's Do Some 'Hesabu' with Blocks!
You can also use blocks for math. Imagine you are at the duka buying 3 mandazis at 10 shillings each and 1 soda at 30 shillings.
Here is how a program would calculate the total cost, step-by-step:
// Step 1: Set up our variables (containers for numbers)
Set costOfMandazi = 10
Set numberOfMandazis = 3
Set costOfSoda = 30
// Step 2: Do the calculations
Set totalMandaziCost = costOfMandazi * numberOfMandazis
// This becomes: 10 * 3 = 30
Set grandTotal = totalMandaziCost + costOfSoda
// This becomes: 30 + 30 = 60
// Step 3: Show the final answer
Display "Your total is KES 60"
See? It's just simple math, broken down into clear steps for the computer to follow.
Image Suggestion: A clean, modern diagram showing the calculation logic. Use icons: a mandazi icon next to "10 KES", a soda bottle icon next to "30 KES". Show blocks for "Set Variable", "Multiply", and "Add", leading to a final block displaying "Total: 60 KES". The style should be like a school textbook diagram, but more engaging and colourful.
Putting It All Together: The Matatu Simulator!
Let's design the logic for a simple game where a matatu drives its route, picking up passengers. This will use all three of our superpowers!
[ START PROGRAM ]
// --- SEQUENCE: Getting ready ---
Move Matatu to "Bus Stop A"
Play "Karibu!" sound
// --- ITERATION: Repeat for 10 passengers ---
REPEAT 10 TIMES:
Wait for a new passenger to appear
// --- SELECTION: Check if there is space ---
IF (Matatu seats are NOT full) THEN
Add passenger to Matatu
Increase passenger_count by 1
ELSE
Display "Sorry, matatu imejaa!" message
END IF
END REPEAT
// --- SEQUENCE: Finishing the trip ---
Drive Matatu to "Town Centre"
Display "Umefika! Trip complete!" message
[ END PROGRAM ]
You Are Now a Logic Master!
Congratulations! You have learned the three fundamental building blocks of all programming: Sequence, Selection, and Iteration. Whether it's a simple animation, a mobile app, or a complex website, all of them are built using these simple ideas. The next step is to open a visual programming tool like Scratch and start building! Don't be afraid to experiment. The best way to learn is by doing. Happy coding!
Habari Future Coder! Let's Build with Logic!
Welcome! Karibu! Have you ever followed a recipe to make delicious chapatis? Or given a friend directions to your home? If you have, you are already a programmer! Programming is simply giving a set of instructions to a computer to complete a task. Today, we are going to learn how to give these instructions in a fun, visual way, just like building with LEGO blocks. Forget typing complicated code for now; let's think and build!
Image Suggestion: An energetic and smiling Kenyan teenager sitting at a desk with a laptop. On the screen is a colourful block-based coding interface like Scratch. The background has subtle Kenyan motifs, like a Maasai shuka pattern on a cushion. The style is bright, optimistic, and slightly stylized digital art.
So, What is Programming Logic?
Programming logic is the master plan or the "thinking" behind any program. It’s the way we arrange our instructions in a sensible order to get the correct result. Think about using M-Pesa:
You can't enter the amount you want to send before you enter the person's phone number, right? The system has a specific, logical order you must follow. That order is its programming logic!
- Step 1: Go to M-Pesa menu.
- Step 2: Select 'Send Money'.
- Step 3: Enter Phone Number.
- Step 4: Enter Amount.
- Step 5: Enter PIN.
- Step 6: Confirm.
If you mix up this order, the transaction will fail. Computers need this same level of clear, step-by-step logic.
With Visual or Block Programming, we represent these logical steps using colourful blocks that we drag and drop. Each block is an instruction, and we snap them together to build our program. It’s a fantastic way to focus purely on the logic without worrying about spelling mistakes or commas!
The Basic Building Blocks of Logic
Almost every program you will ever see uses a combination of these three simple ideas. Let's break them down with some examples from home.
1. Sequence: The Order of Things
A sequence is a series of instructions that the computer follows one after the other, from top to bottom. Just like the steps for making your morning tea. You must do them in the right order!
Simple Tea-Making Logic (A Sequence)
START
|
V
[ Boil Water ]
|
V
[ Add Tea Leaves (Majani) ]
|
V
[ Pour in Milk ]
|
V
[ Add Sugar ]
|
V
[ Serve Tea ]
|
V
END
2. Conditionals (IF...THEN...ELSE): Making Choices
Life is full of choices, and so are computer programs. A conditional is a decision point. It checks if a certain condition is true, and then acts accordingly. We use words like IF, THEN, and ELSE.
Scenario: You are about to leave for school. You look outside.
IF it is raining, THEN you will take an umbrella. ELSE (if it's not raining), you will leave the umbrella at home.
This is how a computer makes a decision:
The "Umbrella" Logic (A Conditional)
[ Is it raining? ]
/ \
/ \
(IF YES) (IF NO / ELSE)
/ \
V V
[ Take Umbrella ] [ Leave Umbrella ]
In block programming, this would look like a block with a C-shape where you can put other blocks inside!
Image Suggestion: A digital illustration showing a fork in a path on a Kenyan street. One path leads to a sunny scene with a student walking happily without an umbrella. The other path leads to a rainy scene where the same student is walking under a bright yellow umbrella. The sign at the fork says "IF RAINING?". This visually represents the IF/ELSE choice.
3. Loops: Doing Things Again and Again
Imagine telling a robot to plant 100 maize seeds in your shamba (farm). Would you say "Plant seed, move one step, plant seed, move one step..." 100 times? That's too much work! A loop lets us give an instruction and tell the computer to repeat it a certain number of times.
Instead, you would just say:
REPEAT 100 TIMES:
[ Plant one seed ]
[ Move one step forward ]
This is a loop! It saves time and makes your code much shorter and smarter. In block-based coding, this is often a block that looks like it's "hugging" the other blocks you want to repeat.
Let's Code! Your First Block Program
Sawa! Let's put this all together. Imagine we have a character of a Boda Boda on the screen. We want it to move across the screen and greet us. Here is how we would build the logic with blocks.
Our Goal: When we click the green "Start" flag, our Boda Boda moves 150 steps forward and then says "Nimefika!" (I have arrived!). This is a simple sequence.
Here is what the blocks would look like, snapped together:
+------------------------------+
| WHEN (Green Flag) CLICKED | <-- This is the event/trigger
+------------------------------+
| MOVE (150) STEPS | <-- First action in the sequence
+------------------------------+
| SAY "Nimefika!" FOR 2 SECONDS | <-- Second action in the sequence
+------------------------------+
See? It’s just like building a sentence. Simple, clear, and logical!
Your Turn: The Boda Boda Challenge!
Now it's your chance to be the programmer. Using the logic we learned, think about how you would solve this problem:
How would you change the program to make the Boda Boda move forward, wait for a passenger (wait 3 seconds), and then move forward again? And for a bigger challenge, how would you use a loop and a conditional to make the Boda Boda go back and forth across the screen forever, but only IF it has fuel?
Don't worry about getting it perfect. The most important part is thinking through the steps logically. That is the real heart of coding.
Hongera! You have just learned the fundamental logic that powers everything from websites to video games to the M-Pesa app in your pocket. By starting with blocks, you are building a strong foundation to become an amazing problem-solver and developer. Keep practicing, stay curious, and have fun building!
Pro Tip
Take your own short notes while going through the topics.