Skip to the page
Year 7 · Spring 2 · Scratch I: Programming essentials

Selection: if / if-else and conditions

PracticalPresent ▶

What this lesson covers

Topic 11 Algorithms

KS3 Computing · An algorithm is a precise, step-by-step plan to solve a problem. Computational thinking turns a messy problem into one. Every algorithm is built from just three things: sequence, selection and iteration.

Programming Computational ThinkingCambridge Lower Secondary Computing 0860

You will learn to

  • Explain what a condition is: a question with a true or false answer.
  • Use if … then to run blocks only when a condition is true.
  • Use if … then … else to choose between two paths.
  • Identify the diamond shape that shows a decision in a flowchart.

Success criteria

  • I can make a sprite react when it touches the edge.
  • I can make a sprite say 'You win' when the score is more than 10.
  • I can predict which path an if-else block will take.

Key words

Say each word, then read what it means. Every one is in the glossary too.

Algorithm
Precise, ordered, finite steps to solve a problem.
Decomposition
Breaking a problem into smaller ones.
Abstraction
Ignoring detail that doesn't matter for the problem.
Sequence · selection · iteration
Steps one after another. First this, then this.
Pseudocode
Code-like writing for algorithms, without strict rules.
Trace table
Records variable values step by step to check logic.
Variable
A named place in memory that holds a value.

The explanation

Read it here, or open the study page below to go one step at a time with things to try.

Conditions: yes or no questions

Selection means the program chooses what to do next.

It chooses by asking a condition. A condition is a question with only two answers: yes or no.

Computers say true for yes and false for no.

  • Is the sprite touching the edge?
  • Is the score more than 10?
  • Is the space key pressed?
★Remember

In Scratch, conditions are the pointed, six-sided blocks. They only fit into the pointed holes.

if … then: do it only when true

when this sprite clicked
change [score v] by (1)
if <(score) > (10)> then
    say [You win!]
end
  1. The sprite is clicked. The score goes up by 1.
  2. Scratch asks: is the score more than 10?
  3. True: it runs the blocks inside, so the sprite says 'You win!'.
  4. False: it skips the blocks inside and carries on.
e.g.Example

Bounce off the edge: if <touching (edge v)?> then put turn right (180) degrees inside.

if … then … else: two paths

ask [What is 6 × 7?] and wait
if <(answer) = (42)> then
    say [Correct!]
else
    say [Try again.]
end

The answer block holds what the player typed.

If the condition is true, the top path runs. If it is false, the else path runs.

★Remember

With if … else, exactly one path runs. Never both, never neither.

In a flowchart, a decision is a diamond. Two arrows leave it, labelled Yes and No.

Comparing numbers in Scratch

BlockAsksExample
( ) > ( )Is the first more than the second?12 > 10 is true
( ) < ( )Is the first less than the second?12 < 10 is false
( ) = ( )Are they the same?10 = 10 is true
!Watch out

Scratch has no 'more than or equal to' block. 10 > 10 is false. To include 10, use score > 9 for whole numbers.

e.g.Worked example

The score is 10. The script says: if score > 10 then say 'You win!'. Does the sprite say anything?

  1. Ask the condition: is 10 more than 10?
  2. No. 10 is equal to 10, not more than it.
  3. So the condition is false, and Scratch skips the say block.
  4. To win at 10, change the condition to score > 9.

Study it

Open the study page and go one step at a time.

Check yourself

One question at a time. The reason shows after each answer.

Saved on this device.