Consica Labs

Consica Labs
Chapter 8

Loops

Repeating actions using count and forever loops

Definition

Loops are control structures (Repeat, Forever) that rerun a stack of attached instructions multiple times. Key concepts include Hat Block.

Think of Loops as:

Director script
Grid movements
Trigger alarms
Costume racks

Just as an actor changes costume or walks across the theater grid on cue, Loops dictates sprite behavior.

Real-Life Example

Just as you coordinate actions in real life, Loops works by structuring logic commands in sequence:

  1. 1 Register the trigger event block.
  2. 2 Run calculations or movements within a continuous control block.
  3. 3 Update values or graphics on the stage viewport.

Key Highlights:

  • Trigger event
  • Logic evaluation
  • Visual update

Interactive Diagram

Launch the interactive diagram to see this in action.

Open Interactive Diagram

The interactive diagram for this chapter demonstrates Loops. It shows loop blocks (repeat, forever, repeat until) showing how code runs multiple times.

What to explore:

  • click to run each loop type; watch the blocks inside highlight each time they execute; see the count change
  • loops repeat a set of instructions — use "forever" for continuous actions and "repeat" for a fixed number of times

Introduction

Computers are very good at doing things over and over again without getting bored or making mistakes. In programming, we use Loops to repeat actions. Loops are one of the most powerful concepts in computer science because they let a small amount of code do a large amount of work. Scratch provides several types of Loops to handle different repetition needs.

Loops in Scratch are found in the Control category (orange blocks). There are three main loop blocks: 'repeat ()' (run a fixed number of times), 'forever' (run endlessly), and 'repeat until ()' (run until a condition becomes true). Each type serves a different purpose, but they all follow the same basic idea — run a set of blocks multiple times without writing them twice.

In this chapter, you will learn how to use each type of loop, understand when to use one kind over another, and practice common loop patterns used in games and animations. By the end, you will be able to make your code shorter, cleaner, and more powerful with Loops.

How It Works

The 'repeat (10)' block runs its contents a specific number of times. You type the number (or use a variable) in the input field. The block has a C shape — you place other blocks inside the C to indicate what should be repeated. For example, putting a 'move (10) steps' block inside a 'repeat (10)' block makes the sprite move 100 steps total (10 × 10 = 100). The number of repetitions can be any positive integer.

The 'forever' block runs its contents endlessly until the project stops or a 'stop all' block is executed. This is the most commonly used loop in Scratch because games and animations typically need to keep running and checking for input continuously. Inside a 'forever' loop, you typically place a sequence that moves sprites, checks for collisions, or updates the display — then the loop repeats it all again.

Household Object Analogy

Think of Loops like a playlist on repeat. A 'repeat (5)' loop is like telling your music player to play a particular song 5 times and then stop. A 'forever' loop is like putting your playlist on infinite shuffle — it keeps going until you press the stop button. A 'repeat until' loop is like listening to a podcast until you reach the end — you keep listening until a specific condition is met.

Deeper Dive

The 'repeat until ()' block combines a loop with a condition. It checks the condition (a true/false expression) before each repetition. If the condition is false, it runs the loop body once, then checks again. When the condition becomes true, it exits the loop and continues with the blocks after the loop. This is useful for 'keep doing something until something happens' patterns, like moving a sprite until it reaches a target position.

Nested Loops — a loop inside another loop — can create complex patterns from simple code. For example, a 'repeat (4)' loop containing another 'repeat (4)' loop with 'move' and 'turn' blocks can draw a grid of squares. The outer loop runs 4 times, and each iteration the inner loop draws one square by running 4 times. Combined: 4 × 4 = 16 line segments drawn with just a few blocks.

Loop efficiency matters in Scratch. A 'forever' loop that contains heavy operations (like graphic effects or complex calculations) can slow down your project. Scratch runs at 30 frames per second, so each loop iteration should complete quickly. If your project becomes laggy, look for Loops doing too much work per iteration. Moving non-repeating code outside the loop is a common optimization.

Key Insight

The 'repeat (10)' block is actually a 'count-controlled loop' in computer science. It is called that because you control exactly how many times it runs using a counter. The 'repeat until' is a 'condition-controlled loop' because it runs until a condition changes. Understanding this distinction helps you choose the right loop for each situation.

Advanced

Scratch Loops run on the same 30 FPS frame rate as the rest of the project. A single iteration of a 'forever' loop happens every frame (about 33 milliseconds). This means that in 1 second, a 'forever' loop runs approximately 30 times. The actual speed can vary if there are many sprites or complex calculations, but 30 FPS is the target that Scratch tries to maintain.

The 'repeat until' loop is equivalent to a 'while not' loop in text-based programming languages like Python or Java. In Python, the same logic would be 'while not condition:' with the loop body indented below. Understanding this connection helps when you transition from Scratch to text-based languages.

Infinite Loops without any control block inside can freeze your project. For example, a 'forever' loop without any wait, movement, or Sensing Block will still work, but it may prevent other scripts from running smoothly. Always include at least one block inside your Loops that changes something — a variable update, a motion block, or a 'wait () seconds' block — to give Scratch's scheduler a chance to interleave other scripts.

Vocabulary Table

Term Definition
LoopsThe primary technological concept explaining how components interact within the context of Scratch Programming.
Coordinate GridThe X and Y plane representing the Stage layout coordinates.
Hat BlockA block with a rounded top that registers events to launch scripts.
Sensing BlockA block that checks for collisions, touch parameters, or mouse pointer coordinates.

Fun Facts

The 'forever' block is the most used block in all Scratch projects. Nearly every game and animation uses at least one forever loop.

A 'repeat (0)' block skips the contents entirely — it runs zero times. This can be useful for temporarily disabling code without deleting it.

The 'repeat until' block was added in Scratch 2.0. Before that, programmers had to use a 'forever' loop with an 'if' block and a 'stop this script' block to achieve the same effect.

Scratch's loop blocks can contain any other blocks, including other Loops. There is no limit to how deeply Loops can be nested, though performance may degrade with very deep nesting.

The 'stop' block (in Control) can stop 'all', 'this script', or 'other scripts in sprite'. Using 'stop this script' inside a loop exits the script entirely, which is one way to break out of a 'forever' loop.

Common Misconceptions

Misconception: A 'forever' loop runs exactly 100 times per second.

Truth: The 'forever' loop runs approximately 30 times per second (once per frame), matching Scratch's 30 FPS frame rate.

Misconception: All Loops are interchangeable.

Truth: Different Loops serve different purposes. Use 'repeat' when you know the count, 'forever' for continuous operation, and 'repeat until' when the end condition depends on something that changes during the loop.

Misconception: Loops are only for animation and movement.

Truth: Loops are used for many purposes beyond animation: repeating checks (waiting for input), processing list items, creating patterns, and automating repetitive calculations.

Misconception: Nested Loops multiply the time proportionally.

Truth: If the inner loop runs 10 times and the outer loop runs 10 times, the innermost code runs 100 times. Nested Loops multiply, not add, which is why deeply nested Loops can become slow.

Knowledge Check

1. What is the main role of Loops?

Answer: To orchestrate behaviors and controls visually

2. Which block shape starts a script stack in Scratch?

Answer: Hat block (curved top)

3. What is the maximum X coordinate limit on the stage?

Answer: 240