Grade 8Pre-Technical Studies

Visual Programming

Block-based programming using Scratch/Blockly; algorithms, sequences, loops, events.

📖 5 min read · 3 worked examples · 5 practice questions

📚 Practise Visual Programming with the AI tutor
Free email sign-in · AI tutor in English, Kiswahili or Sheng
Get started →

The lesson

Today we are beginning our journey into visual programming. By the end of this lesson you will understand why block‑based tools like Scratch are useful, and you'll be ready to build simple programs. First, let's look at our learning objectives. We will: 1) understand the purpose of block‑based visual programming; 2) identify key concepts such as algorithms, sequences, loops, and events; and 3) create a basic program using Scratch or Blockly. Why use blocks instead of text code? Imagine building a LEGO house – each block snaps together safely, so you can focus on the design without worrying about syntax errors. That's exactly what visual programming gives us. Think of an algorithm as a recipe. It tells the computer step‑by‑step what to do, just like a cooking instruction tells you how to make ugali. We'll explore sequences, loops, and events shortly, and then each of you will create a simple animation in Scratch.

Today we'll explore what an algorithm really is and see how it looks in Scratch. An algorithm is a finite set of ordered instructions that solves a problem. Think of it as a recipe you follow step by step until the dish is ready. Here's the beginning of our simple Scratch script: the "Start" block tells the computer where to begin. Next, the instruction "move forward 10 steps" tells the sprite exactly what to do – that's one step of our algorithm. Finally, the "End" block marks the completion of the algorithm. Every Scratch script follows this same start‑middle‑end pattern, just like any well‑written set of instructions.

Today we'll explore how the order of blocks determines what our program does. First, notice this bullet: blocks run from top to bottom. Think of it like stacking bricks—each brick rests on the one below, so the program follows that stack. The Kenyan market‑day scenario. If we arrange the actions correctly—Buy, then Transport, then Sell, then Profit—the market runs smoothly. Here you can see the correct sequence compared to an incorrect one. Changing the order breaks the flow, just like skipping a step in a real market would cause loss. Remember: the stack order is the program's recipe. Rearrange the blocks, and you change the outcome. Any questions before we move on?

Everyone, let's dive into loops – the tool that lets us repeat actions without writing the same code over and over. First, notice this bullet point: a loop block repeats the commands inside it a set number of times. Think of it like a dance routine that you tell the sprite to perform again and again. Common loop types are 'repeat 10', 'repeat until', and 'forever'. 'Repeat 10' runs exactly ten times, 'repeat until' keeps going until a condition becomes true, and 'forever' never stops unless we tell it to. For example, imagine a school celebration where a sprite needs to dance continuously. We would place the dance commands inside a 'forever' loop so the sprite keeps moving while the music plays. Any questions so far? Remember, loops help us keep our code tidy and efficient, especially when we need repeated actions.

Everyone, let's dive into the heart of Scratch programming – Events, the blocks that kick off our code when something happens. First, we have the three basic event blocks: "when flag clicked", "when key pressed", and "when this sprite clicked". Each of these tells Scratch, "Start this script when the user does this specific action." For example, the green flag is like the start button on a TV remote – click it and the whole program begins. When we use "when key pressed", we can make a sprite move when a student presses the space bar, just like pressing a button to launch a drone. "when this sprite clicked" lets us create interactive quizzes – the student clicks the answer sprite, and the script checks if it's correct. A real‑world Kenyan example: imagine a rain‑alert system. When a sensor sends a value indicating heavy rain, a broadcast message can trigger a "start rain‑alert animation" event, alerting the whole community. Remember, events are the bridges between the outside world and our code, turning user actions or sensor data into lively programs.

Worked examples

– Counting Cattle

Everyone, let's walk through our first worked example: counting cattle on a farm using a simple program. First, we create a variable called cattleCount. This will store the total number of cows we have. Next, we use a repeat loop that runs once for each cow sprite on the screen, adding one to cattleCount each time. Finally, we make the sprite say the value of cattleCount, so the program shows the total count. This mirrors what many Kenyan farmers do each morning—counting their cattle before heading out to the fields.

– Rainfall Alert System

Everyone, let's dive into Worked Example 2 – the Rainfall Alert System. We'll see how a simple event‑driven program can warn us when rain gets too heavy. First, we use a 'when I receive' broadcast to grab the sensor input. Think of it as the rain sensor shouting, 'Hey, I have a new reading!' to the rest of the program. Next, we place a conditional block – an if‑then check – that compares the reading to our threshold. If the rain is above that level, the program follows the 'then' path. When the condition is true, we play a sound and switch the backdrop to a cloudy sky, giving a clear visual and audible alert. Finally, notice how this small script mirrors real‑world weather monitoring stations across Kenya, helping farmers and communities prepare for heavy rain.

– Market Day Timer

Everyone, let's dive into Worked Example 3 – the Market Day Timer. We'll build a simple countdown that signals when the market opens. First, we set a timer variable to 60 seconds. This gives us a one‑minute countdown before the market starts. Next, we use a 'repeat‑until' loop that subtracts one second each cycle until the timer reaches zero. When the timer hits zero, we broadcast a 'market start' event so all sprites know it's time to begin. Finally, we show a celebration animation as a reward—just like the fireworks you see on market opening days. Let's pause for a moment. Does anyone have a question about how the loop works or why we use a broadcast?

Practice questions

  • Remember: the order of blocks matters. For movement, the "when green flag clicked" hat must start the script, followed by any positioning blocks, then the action like "move 10 steps".
  • When you need a task to happen a specific number of times, the "repeat _ times" block is the right choice. It runs the inner blocks exactly that many iterations, so option A is the only correct way to turn 15 degrees five times.
  • To start a program with a keyboard press, use the "when [key] key pressed" event block. For the space bar, that's option B.
  • For the short‑answer question, picture three blocks stacked: first a "say \"Hello!
  • Take a moment to think about each question, write your answer clearly, and don't worry about perfection—you've already built a solid foundation.

Ask the tutor

  • Explain Welcome & Learning Objectives in simple words.
  • Give me 3 worked examples on Welcome & Learning Objectives.
  • Quiz me with 5 questions on Welcome & Learning Objectives.
  • What's the most common mistake students make on Welcome & Learning Objectives?
Sign up for a CBC AI tutor →

Free email sign-up — the tutor answers in English, Kiswahili or Sheng and walks you through visual programming step by step.

Keep going in Pre-Technical Studies5 more