Menu
Theme

Grade 8
Course Content
View Overview

Programming logic (Visual/Block)

Coding

Habari Mwanafunzi! Welcome to the World of Programming Logic!

Ever wondered how a game knows when you've scored a goal, or how an app like M-Pesa knows if you have enough money for a transaction? It’s not magic, it’s logic! Think of it like giving instructions to your friend on how to make a perfect cup of chai. You have to give them the right steps in the right order. Programming is just the same, but you are giving instructions to a computer.

Today, we will learn how to "think" like a computer using Visual or Block Programming. Instead of writing complicated text, we will use colourful blocks that snap together like LEGOs. It's fun, creative, and the best way to start your coding safari!

Image Suggestion: A vibrant and colourful digital illustration of a Kenyan student sitting at a computer, smiling. On the screen, they are dragging and dropping colourful code blocks that look like LEGOs to build a simple animation of a bouncing soccer ball. The background has subtle hints of Kenyan patterns.

The Three Superpowers of Programming Logic

All complex programs, from the games you play to the websites you visit, are built using three basic logical structures. Once you master these, you can build almost anything! Let's call them our coding superpowers.

  • Sequence: The art of doing things in order.
  • Selection: The power to make choices.
  • Iteration (Loops): The magic of repeating actions.

1. Sequence: The "Do This, Then That" Logic

A sequence is a series of instructions that the computer follows one by one, from top to bottom, without skipping any. Just like following a recipe to cook ugali, the order is very important!

Real-World Example: Sending a text message (SMS)
  1. Unlock your phone.
  2. Open the messaging app.
  3. Select a contact.
  4. Type your message.
  5. Press 'Send'.

You can't press 'Send' before you type the message. That's a sequence!

In block programming, you would just stack the blocks one on top of the other.


ASCII Diagram: A Simple Sequence Flowchart

      [START]
         |
         V
  [Step 1: Unlock Phone]
         |
         V
  [Step 2: Open App]
         |
         V
  [Step 3: Type Message]
         |
         V
       [END]

2. Selection: The "If This, Then That" Logic

Life is full of choices, and so is programming! Selection allows a program to make a decision based on a condition. The most common way to do this is with an IF...THEN...ELSE statement.

  • IF a condition is true, THEN do something.
  • ELSE (if the condition is false), do something else.
Kenyan Example: Crossing the road in Nairobi

Imagine you are at a zebra crossing. Your brain quickly makes a decision:

IF the traffic light is green for pedestrians, THEN you cross the road. ELSE (if it's red), you wait.

The computer does the exact same thing!

Image Suggestion: A split-screen cartoon. On the left, a "IF" block shows a green pedestrian light, and a character is happily walking across a zebra crossing. On the right, an "ELSE" block shows a red pedestrian light, and the same character is waiting patiently on the pavement.


ASCII Diagram: A Selection Flowchart

          [START]
             |
             V
      <Is light green?> --No--> [Wait on pavement]
             |
            Yes
             |
             V
      [Cross the road]
             |
             V
           [END]

3. Iteration (Loops): The "Repeat" Logic

Why write the same instruction 100 times? That's tiring! Iteration, or looping, allows us to repeat a block of code over and over again until a certain condition is met. It’s the secret to making things efficient.

Kenyan Example: A farmer planting seeds in a shamba

A farmer doesn't have a unique instruction for every single seed. They have a process they repeat:

REPEAT this process for every seed:

  • Dig a small hole.
  • Place the seed inside.
  • Cover the seed with soil.

This loop continues until all the seeds are planted. That's iteration!

In block programming, you use a "repeat" or "forever" block and place the actions you want to repeat inside it.


Block Programming Example: Making a character jump 5 times

REPEAT 5 times:
  MOVE character up by 50 pixels
  WAIT 0.5 seconds
  MOVE character down by 50 pixels
  WAIT 0.5 seconds

Let's Put It All Together: A Matatu Ride!

Imagine we want to create a simple animation of a matatu moving across the screen and stopping when it sees a passenger.

  1. Sequence: The matatu must appear on the left side of the screen first.
  2. Iteration: We need to REPEAT a "move forward" command to make it look like it's driving.
  3. Selection: Inside our loop, we add a check: IF the matatu is touching the 'passenger' sprite, THEN stop the loop and say "Hoot! Hoot! Tumefika!".

Visual Block Logic:

[WHEN a green flag is clicked]
  [GO TO start position (x:-200, y:0)]

  [REPEAT UNTIL (touching 'passenger'?)]
     [MOVE 10 steps]
     [WAIT 0.1 seconds]

  [SAY "Hoot! Hoot! Tumefika!" for 2 seconds]

You've Got This!

See? You are already thinking like a programmer! These three superpowers—Sequence, Selection, and Iteration—are the foundation of everything you will do in coding. By mastering how to combine them, you can solve problems and build amazing things.

Now, go and explore a visual programming tool like Scratch. Try to build your own stories and games using these logic blocks. Kazi nzuri na kila la kheri! (Good work and all the best!)

Jenga na Code: Your First Step into Programming Logic!

Habari Class! Welcome to the exciting world of coding. Before we can write complex applications like M-PESA or build amazing websites, we need to learn the secret language of computers. But don't worry, it's not about typing confusing words right away. It's about thinking like a computer. Think about giving your friend directions to the best chapati spot in town. You have to be clear, step-by-step, right? That's exactly what programming logic is!

Today, we're going to learn this logic using something fun and easy: Visual or Block-based programming. Imagine building with LEGO blocks, but instead of a house, you're building a program. Let's begin our journey to becoming coding wizards! Sawa?

What is Programming Logic? (The 'Aha!' Moment)

Programming logic is the brain behind any computer program. It's the plan, the recipe, the set of instructions that tells the computer what to do, how to do it, and when to do it. It’s the common sense of coding.

Real-World Example: Making Ugali

Think about making ugali. You can't just throw flour and water in a pot and hope for the best! There's a specific order, a logic:

  1. Boil the water.
  2. Slowly add the maize flour.
  3. Stir continuously until it's firm.
  4. Let it cook for a few minutes.
  5. Serve!

If you mix up these steps, you get a mess, not ugali. A computer is the same—it needs a perfect, logical recipe to work correctly. Block programming helps us build that recipe without any typos.

The Core Building Blocks of Logic

Every program, from a simple game to a complex banking app, is built using three basic ideas. Let's call them our "Power Blocks".

1. Sequence (Hatua kwa Hatua - Step by Step)

A sequence is a series of actions that are performed in a specific order, one after the other. Just like the ugali recipe, the order matters!


ASCII Art: Simple Sequence Flow

[ START ]
    |
    V
[ Step 1: Greet the User ]
    |
    V
[ Step 2: Ask their name ]
    |
    V
[ Step 3: Say "Karibu, [Name]!" ]
    |
    V
[  END  ]

2. Conditionals (Kama... Basi... - If... Then...)

This is where the computer makes decisions. It checks if a certain condition is true or false and then acts based on the answer. This is how apps respond to you!

Kenyan Example: Deciding What to Wear

Imagine you're getting ready for school. You look outside. IF it is raining, THEN you will carry an umbrella. ELSE (if it's not raining), you will leave the umbrella at home. That's a conditional statement!

Image Suggestion:

A vibrant, colourful digital art illustration of a Kenyan student in school uniform standing at their doorway. One side of the image shows a window with heavy rain, and the student is picking up a bright yellow umbrella. The other side shows the same window with a bright sun, and the student is leaving the umbrella on a stand. The style should be cheerful and educational.


ASCII Art: A Decision Block

          +------------------+
          | Is it raining?   |
          +------------------+
                 /      \
           (YES) /        \ (NO)
               /          \
              V            V
  +-----------------+  +----------------------+
  | Carry Umbrella  |  | Leave Umbrella at Home |
  +-----------------+  +----------------------+

3. Loops (Tena na Tena - Again and Again)

A loop is used to repeat a set of actions multiple times without you having to write them out over and over again. It's for being efficiently lazy, which is what great programmers do!

Kenyan Example: Buying Mandazi at the Duka

You want to buy 4 mandazis, and each costs 10 shillings. You could tell the computer: "Add 10, then add 10, then add 10, then add 10." Or, you could use a loop and say: "REPEAT 4 times: Add 10 shillings to the total." Much easier!


Step-by-Step Calculation using a Loop Concept:

Let total_cost = 0
Let price_per_mandazi = 10
Let number_of_mandazis = 4

Loop starts (Repeat 4 times):
  - Iteration 1: total_cost = 0 + 10  = 10
  - Iteration 2: total_cost = 10 + 10 = 20
  - Iteration 3: total_cost = 20 + 10 = 30
  - Iteration 4: total_cost = 30 + 10 = 40
Loop ends.

Final Answer: total_cost = 40 shillings
Image Suggestion:

A split-screen cartoon. On the left, a tired-looking robot is shown writing the same line of code "Jump()" over and over again on a long scroll of paper. On the right, a happy, energetic robot is shown placing a single "Repeat 10 times: Jump()" block into a program, looking relaxed. The background is a clean, modern coding environment.

Let's Build! A Practical Example

Let's design a simple program for a farmer in Makueni to help them decide if they should water their shamba (farm).

The Problem: The farmer only wants to water the crops if the soil is dry AND it is sunny outside (to avoid wasting water if it's about to rain). Our Logic Plan:
  1. First, we check if the soil is dry.
  2. If it is, THEN we check if the sun is shining.
  3. Only if BOTH are true, we water the shamba.

Here is how that logic would look using visual blocks:


[ WHEN PROGRAM STARTS ]
 |
 +--> [ IF soil is 'dry' ] THEN
         |
         +--> [ IF sky is 'sunny' ] THEN
         |       |
         |       +--> [ ACTION: Turn on the water sprinklers! ]
         |
         +--> [ ELSE ]
                 |
                 +--> [ ACTION: Do nothing. Wait for the sun. ]
 |
 +--> [ ELSE ]
         |
         +--> [ ACTION: Do nothing. Soil is wet. ]

See how we combined a sequence (checking one thing after another) and conditionals (the IF blocks) to solve a real-world problem? That is the power of programming logic!

Conclusion: Wewe ni M-coder Sasa! (You are a Coder Now!)

Congratulations! You have just learned the three fundamental building blocks of all programming: Sequence, Conditionals, and Loops. Starting with visual blocks is the best way because it lets you focus on the *thinking* part of coding, not the typing.

The logic you used to decide about watering a shamba is the same core logic used to build games, animate movies, and run the apps you use every day. You have taken the most important step on your coding journey. Keep practicing this way of thinking, and soon you'll be building amazing things. Well done!

Habari Mwanafunzi! Welcome to the World of Programming Logic!

Ever wondered how apps like M-Pesa know if you have enough money, or how a video game knows when you've scored a goal? It’s not magic, it’s logic! Think of it like giving directions to a friend to get to your place from the bus stage. You have to give them clear, step-by-step instructions in the correct order. "First, walk past the supermarket. Then, turn left at the big acacia tree. If the gate is closed, press the buzzer." That's exactly what programming logic is – creating a perfect set of instructions for a computer to follow.

Today, we are starting this exciting journey with Visual or Block Programming. Forget about confusing text and scary punctuation for now. We are going to build programs like we build with LEGO blocks – fun, colourful, and super creative!

Image Suggestion: A vibrant and colorful illustration of a Kenyan student sitting under an acacia tree, happily arranging colorful coding blocks on a laptop screen. In the background, a giraffe peeks curiously over a fence. The style should be modern, cheerful, and inspiring.

What is This 'Programming Logic' Anyway?

Programming Logic is the foundation, the mabati and poles of our coding house. It’s the plan you make before you start building. It involves three main ideas that you already use in your daily life!

  • Sequence: The order of steps. Just like making ugali, you must boil the water before you add the unga. If you mix up the sequence, you'll have a mess! In programming, the computer follows your instructions one by one, in the exact order you set them.
  • Selection (Decisions): Making choices. This is your "if... then... else..." thinking. For example, if your phone battery is below 20%, then you look for a charger, else you continue using it. The program makes a decision based on a condition.
  • Iteration (Loops): Repeating actions. Think about a matatu conductor shouting "Tao! Tao! Tao!" He repeats this action until the matatu is full. In programming, a loop allows us to repeat a set of instructions multiple times without writing them over and over again.

Building with Blocks: Your First Tools

Visual programming platforms like Scratch and Blockly use colourful blocks that snap together. Each block represents a command or an action. Because they only fit together in ways that make sense, it's very hard to make a mistake! It's all about putting the right blocks in the right order.

Imagine these blocks:


+---------------------+
|  When Flag Clicked  |  <-- This starts the program
+---------------------+
         |
         | Snaps to
         v
+---------------------+
|     Move 10 steps   |  <-- This is an action block
+---------------------+
         |
         | Snaps to
         v
+---------------------+
|      Say "Jambo!"   |  <-- Another action block
+---------------------+

Let's Solve a Kenyan Problem: The KCSE Pass Mark Checker

Let's create a simple program to check if a student's mark in a subject is a passing grade. We'll say the passing mark is 50 for this example.

1. The Logic (The Plan)

  1. The program will ask for the student's mark.
  2. The program will look at the mark (input).
  3. (Selection) It will check: Is the mark 50 or higher?
  4. If the answer is YES, it will display a success message: "Hongera! You passed!"
  5. If the answer is NO, it will display an encouraging message: "Usijali, keep working hard!"

2. The Flowchart (Visual Plan)

A flowchart helps us see the logic clearly. The diamond shape (<>) is where a decision is made.


[ Start ]
    |
    v
[ Ask for student's mark ]
    |
    v
< Is mark >= 50 ? >----(No)---->[ Display "Usijali!" ] --+
    |                                                      |
   (Yes)                                                     |
    |                                                      |
    v                                                      |
[ Display "Hongera!" ] <-------------------------------------+
    |
    v
[ End ]

3. The Block Code (How it would look in Scratch)

This is how you would snap the blocks together to make our program work. Notice the `if-then-else` block which looks like a C-shape, wrapping around other blocks.


WHEN GREEN FLAG CLICKED

ASK "What was your mark?" AND WAIT

IF < [answer] >= [50] > THEN
    SAY "Hongera! You passed!" FOR 2 SECONDS
ELSE
    SAY "Usijali, keep working hard!" FOR 2 SECONDS
END IF

Image Suggestion: A clear, annotated screenshot of the Scratch interface. The blocks for the "KCSE Pass Mark Checker" should be assembled in the scripting area. Use arrows and labels to point out the 'Event' block (When flag clicked), the 'Sensing' block (ask), the 'Control' block (if-then-else), and the 'Looks' blocks (say).

Adding Repetition: The Safari Jeep Loop

Now, let's use a loop! We want to make a safari jeep sprite move across the screen in short bursts, five times.

Real-World Scenario: This is like a farmer planting maize. They don't have a separate instruction for each seed. They have one set of instructions ("dig hole", "drop seed", "cover seed") that they repeat for every seed they want to plant in a row.

Here’s how the blocks for our safari jeep would look. The `repeat` block will run all the blocks inside it for the specified number of times.


WHEN GREEN FLAG CLICKED

REPEAT < 5 > TIMES
    MOVE < 20 > STEPS
    WAIT < 0.5 > SECONDS
END REPEAT

SAY "What a great safari!"

Why Start with Blocks?

You might be thinking, "This is cool, but when do we get to the 'real' coding?" Sawa sawa, that's a great question! Learning with blocks is like learning to ride a bicycle with training wheels.

  • It lets you focus 100% on the logic (the plan) without worrying about typos or remembering complex symbols.
  • The skills you learn here – sequence, selection, and iteration – are the exact same skills you will use in advanced text-based languages like Python, Java, or C++.
  • It builds your confidence and shows you that you can be a programmer!

Your Challenge!

Fantastic work today! You've learned the fundamental building blocks of all programming. You are officially a programmer!

Now it's your turn. Go to a free platform like Scratch and try this challenge:

Create a short animation where one character (a sprite) asks another, "What is your favourite Kenyan food?" and the second character replies with "Githeri!" or "Chapati!" or your own favourite!

Remember to use the blocks we learned about today. Don't be afraid to experiment and have fun. That's what coding is all about. Kazi nzuri!

Pro Tip

Take your own short notes while going through the topics.

Previous Hardware/Software
KenyaEdu
Add KenyaEdu to Home Screen
For offline access and faster experience