← Back to course
Code Crew G3 / Week 02 / Wednesday
3/6
Week 02 Β· Loops

Wednesday

// Doing it again

Wednesday: Loop Code

Rocket types a short program for Nova. "Repeat five times, say hi," he reads. Nova says hi five times, then stops.

"What do you notice about the number?" Raven asks. "It tells Nova how many times."

Next, Rocket puts a loop inside a loop. Some loops even run until something happens.

Let's read each loop and count what it does.

Computers write loops in short code. Read the pseudocode below:

REPEAT 5 TIMES:
    print 'Hi!'

How many times will it print Hi?

Answer:
WHY THIS EXERCISECSTA 1A-AP-10: A REPEAT N TIMES loop runs its body exactly N times, no more, no less.
REPEAT 3 TIMES:
    clap
    stomp

How many claps? How many stomps?

Claps:
Stomps:
REPEAT 4 TIMES:
    REPEAT 2 TIMES:
        jump

Tricky! How many jumps total?

Total jumps:
WHY THIS EXERCISENested loops multiply: outer Γ— inner. Programmers use this for grids, animations, and repeated work.

REPEAT UNTIL

Some loops don't know how many times. They just go UNTIL something happens. Read this:

REPEAT UNTIL the cup is full:
    pour 1 spoon of water

Circle the answer

If the cup holds 8 spoons, how many times does this loop run?

(a) 1 (b) 8 (c) Forever

My answer:
WHY THIS EXERCISEREPEAT UNTIL exits when its condition becomes true. Loops that don't have a stopping condition are bugs.

Now write a REPEAT UNTIL loop for filling a backpack with books until it is heavy.

REPEAT UNTIL ____________________________:
______________________________________
LOOP CODE CHECK
  • Read the question.
  • Tap your answer.
REPEAT 2 TIMES: print Hi. How many Hi lines?
REPEAT 3 TIMES: (REPEAT 3 TIMES: jump). How many jumps?
REPEAT UNTIL the box is empty: take out one toy. The box has 5 toys. How many laps?
Which loop knows its count before it starts?
← Tuesday