Year 7 · Spring 2

Scratch I: Programming essentials · Lesson 3 of 5

Selection: if / if-else and conditions

PracticalTopic 11 Algorithms

Year 7 · Scratch I: Programming essentials · Lesson 3 of 5

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.

Year 7 · Scratch I: Programming essentials · Lesson 3 of 5

Success criteria

You have got it when you can say:

  • 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.

Year 7 · Scratch I: Programming essentials · Lesson 3 of 5

Retrieval starter

Questions from before. Write your answers, then show them.

  1. Last lesson

    What does print("3" + "4") show?

    A An errorB 34C 7

    B. 34

  2. Last unit

    SELECT FirstName FROM Students WHERE Year = 8; returns…

    A Only the first names of Year 8 studentsB The number of Year 8 studentsC Every field of every student

    A. Only the first names of Year 8 students

  3. Earlier this year

    A game feels laggy, but big downloads are fast. The problem is most likely…

    A Low bandwidthB High latencyC Too many IP addresses

    B. High latency

Year 7 · Scratch I: Programming essentials · Lesson 3 of 5

Key words (1 of 2)

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.

Year 7 · Scratch I: Programming essentials · Lesson 3 of 5

Key words (2 of 2)

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 · step 1 of 4 · part 1 of 2

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?

The explanation · step 1 of 4 · part 2 of 2

Conditions: yes or no questions (continued)

★Remember

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

The explanation · step 2 of 4 · part 1 of 2

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.

The explanation · step 2 of 4 · part 2 of 2

if … then: do it only when true (continued)

e.g.Example

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

The explanation · step 3 of 4 · part 1 of 2

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.

The explanation · step 3 of 4 · part 2 of 2

if … then … else: two paths (continued)

★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.

The explanation · step 4 of 4

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.

Year 7 · Scratch I: Programming essentials · Lesson 3 of 5

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.

Year 7 · Scratch I: Programming essentials · Lesson 3 of 5

Quick quiz · 1 of 3

In a flowchart, a decision is a…

  1. A Oval
  2. B Diamond
  3. C Rectangle

B. Diamond

Diamonds have yes/no arrows coming out.

Year 7 · Scratch I: Programming essentials · Lesson 3 of 5

Quick quiz · 2 of 3

The Tube map leaves out real distances. That's…

  1. A Pattern recognition
  2. B Decomposition
  3. C Abstraction

C. Abstraction

Abstraction removes detail that doesn't matter for the problem.

Year 7 · Scratch I: Programming essentials · Lesson 3 of 5

Quick quiz · 3 of 3

How many times does FOR i ← 2 TO 8 repeat?

  1. A 7
  2. B 6
  3. C 8

A. 7

Both ends count: 8 − 2 + 1 = 7 (2, 3, 4, 5, 6, 7, 8).

Year 7 · Scratch I: Programming essentials · Lesson 3 of 5

Exit ticket

Answer on paper before you go. Then check.

  1. Which loop always runs at least once?

    A REPEAT … UNTILB FOR i ← 1 TO 0C WHILE … ENDWHILE

    A. REPEAT … UNTIL REPEAT … UNTIL checks its condition at the end, after the first run.

  2. x ← 4, then x ← x × 2, then x ← x + 1. What is x?

    A 12B 8C 9

    C. 9 4 × 2 = 8, then 8 + 1 = 9. Each line replaces the old value.