Byte by Byte › Book 4: Office Apps and First Programs › Chapter 10: Conditions and Sensing
Chapter 10: Conditions and Sensing
You will learn: if-then and if-then-else blocks in Scratch, Boolean values (true and false), sensing blocks, comparison operators (<, >, =), combining conditions with and, or and not, and putting conditions inside a forever loop so a program keeps checking.
🌟 The Adventure
The Queen had one last wish for the show. "Let the audience join in," she said. "A game at the end: Beautine must catch falling festival stars before they reach the ground."
Memoon made the stars fall with a loop, and Beautine moved with the arrow keys. But the stars fell straight through Beautine as if she were a ghost. Nothing happened when they touched. Stars that reached the ground just sat there in a pile. And Beautine could run right off the side of the stage and disappear.
"The program doesn't notice anything," said Memoon. "It just follows the steps. It needs to look around and make decisions."
Beautine clutched her tail. "You mean it needs to ask questions? Like Am I touching a star? Yes or no?"
"Exactly," said Memoon. "Questions with yes-or-no answers. If yes, do one thing. Else, do another."

💡 Learn With Memoon
Conditions and Boolean values. A condition is a question whose answer is either true or false. True/false values are called Boolean values, after the mathematician George Boole. In Scratch, conditions are the hexagonal blocks, and they fit only into hexagonal slots.
If-then and if-then-else. You met if, then, else unplugged in Book 3. In Scratch they are orange C blocks in Control:
| Block | What it does |
|---|---|
| if ⟨condition⟩ then | Runs the blocks inside only if the condition is true; otherwise skips them |
| if ⟨condition⟩ then … else … | Runs the first set if true, the second set if false. Exactly one set always runs |
An if block checks once, at the moment the script reaches it. To keep checking all the time, put it inside a forever loop. This pattern, forever + if, is at the heart of almost every game.
when green flag clicked
forever
if ⟨touching Star?⟩ then
play sound Pop
say Got one! for 0.5 seconds
Sensing blocks. The light-blue Sensing category lets a sprite find out about the world:
| Sensing block | Reports |
|---|---|
| touching (mouse-pointer / edge / sprite)? | True if touching it |
| touching color ( )? | True if touching that colour |
| key (space) pressed? | True while that key is held down |
| mouse down? | True while the mouse button is pressed |
| distance to ( ) | A number: how far away something is |
| ask ( ) and wait / answer | Asks the user a question and stores what they type |
| timer / reset timer | Seconds since the program started or the timer was reset |
Comparison operators. In the green Operators category, three blocks compare two values and give true or false:
| Operator | Means | Example | Result |
|---|---|---|---|
| < | less than | 3 < 7 | true |
| > | greater than | 3 > 7 | false |
| = | equal to | 5 = 5 | true |
For example, if ⟨(y position) < -150⟩ then asks "has the star fallen below the bottom area?" and if ⟨(timer) > 30⟩ then asks "have 30 seconds passed?".
Combining conditions: and, or, not.
| Operator | True when | Example |
|---|---|---|
| and | Both conditions are true | ⟨key right arrow pressed?⟩ and ⟨(x position) < 220⟩ |
| or | At least one is true | ⟨touching edge?⟩ or ⟨touching Rock?⟩ |
| not | The condition is false | not ⟨touching Star?⟩ |
A truth table shows every possibility:
| A | B | A and B | A or B |
|---|---|---|---|
| true | true | true | true |
| true | false | false | true |
| false | true | false | true |
| false | false | false | false |
Asking the user. ask (What is your name?) and wait shows a box for typing. Whatever the user types is stored in the round answer block. Combine it with join (Operators) to reply: say (join (Hello, ) (answer)). Combine it with if-else to make a quiz:
ask (How many sides does a hexagon have?) and wait
if ⟨(answer) = 6⟩ then
say Correct! for 2 seconds
else
say Not quite. It has 6. for 2 seconds
Random numbers. pick random (1) to (10) (Operators) gives a different whole number each time. It makes games less predictable, for example go to x: (pick random -220 to 220) y: 180 drops a star from a random place along the top.

✍️ Try It Yourself
Activity 1: Catch the star. You will build the Queen's game.
- Add a catcher sprite (try Squirrel if there is one, or any animal) and a Star sprite. Choose a night backdrop.
- Catcher: when green flag clicked → go to x: 0 y: -140 → forever containing two if blocks:
- if ⟨⟨key right arrow pressed?⟩ and ⟨(x position) < 220⟩⟩ then change x by 10
- if ⟨⟨key left arrow pressed?⟩ and ⟨(x position) > -220⟩⟩ then change x by -10
- Star: when green flag clicked → go to x: (pick random -220 to 220) y: 180 → forever containing:
- change y by -5
- if ⟨touching (catcher)?⟩ then → start sound Collect → go to x: (pick random -220 to 220) y: 180
- if ⟨(y position) < -170⟩ then → go to x: (pick random -220 to 220) y: 180
- Play. Does the catcher stop at the edges? Why does the and matter?
- Change -5 to -8. What happens to the difficulty?
Activity 2: Quiz master. Write a three-question quiz with ask and wait and if-then-else. Include one maths question, one question from this book (for example What does BCC stand for?), and one about you. (Scratch compares answers without caring about capital letters.)
Activity 3: Timed challenge. On the stage, add when green flag clicked → reset timer → wait until ⟨(timer) > 30⟩ → stop all. Now your star game lasts 30 seconds.
Activity 4: Truth detectives. Without the computer, decide true or false: (a) 8 > 3 and 2 > 5; (b) 8 > 3 or 2 > 5; (c) not (4 = 4); (d) (10 < 20) and (20 < 30).
Unplugged: Simon Says, with logic. The leader gives commands with conditions: If you are wearing blue AND you have a pet, stand up. If you like mango OR you like chikoo, clap. If you are NOT holding anything, wave. Everyone checks their own conditions.
🎉 Memoon Solves It
Memoon gave the program its questions. The star asked, again and again, Am I touching Beautine? If true, it played a sparkly sound and jumped back to a random spot at the top. Am I below the floor? If true, it started again from the top. Beautine asked Is the right arrow pressed AND am I not at the edge yet? before every step, so she never ran off the stage.
The audience tried it at the dress rehearsal. Pip the sparrow caught nineteen stars in thirty seconds and refused to give the keyboard back.
Beautine fanned herself with her tail. "I make decisions now," she announced. "If someone admires me, then I bow. Else I wait for them to admire me."
❓ Check Your Knowledge
- What is a Boolean value? What shape are Boolean blocks in Scratch?
- What is the difference between if-then and if-then-else?
- Why do games usually put if blocks inside a forever loop?
- Name three sensing blocks and what they report.
- Is 7 < 4 true or false? Is 5 = 5?
- When is A and B true? When is A or B true?
- What does not ⟨touching edge?⟩ report when the sprite is touching the edge?
- Where is the user's typed reply stored after ask and wait?
- What does pick random 1 to 6 do? What real object could it imitate?
- Challenge: Plan a script that says Too hot! if a number is greater than 35, Too cold! if it is less than 15, and Just right! otherwise. (Hint: an if-else inside another if-else.)
📚 Word Bank
- Condition: a question with a true or false answer.
- Boolean: a value that is either true or false.
- If-then / if-then-else: blocks that run code only when a condition is true / choose between two sets of code.
- Sensing: blocks that let a sprite detect touching, keys, the mouse, time and answers.
- Comparison operator: <, > or =, used to compare two values.
- And / or / not: operators that combine or reverse conditions.
- Truth table: a table showing the result of a logic operator for every combination.
- Answer: the block that stores what the user typed.
- Random number: a number chosen by chance within a range.
Keep learning with Byte by Byte
Explore Book 4: Office Apps and First Programs. Find the ebook, purchase details and teaching slides on MemuniCity.
Comments
Post a Comment