---
title: "Programming with Loops and Variables"
description: "Design and debug programs that use repetition, selection and variables: repeat actions with loops, branch with if-blocks, keep score in a variable, and break a bigger problem into smaller parts that c"
canonical: https://lightmysky.com/learn/computing/programming-with-loops-and-variables-mt_9x5zgShXPg
source: https://lightmysky.com/learn/computing/programming-with-loops-and-variables-mt_9x5zgShXPg.md
retrieved: 2026-10-19
---

> **Agent view.** This is the Markdown twin of the page, for tools and assistants.
> When to use this site, and the call that answers each job: https://lightmysky.com/agent-instructions.md
> API description (OpenAPI 3.1): https://lightmysky.com/openapi.json · Authentication: https://lightmysky.com/auth.md
> Pricing: https://lightmysky.com/pricing.md · Catalog: https://lightmysky.com/llms.txt · Full catalog: https://lightmysky.com/llms-full.txt
> Every machine-readable file on this domain: https://lightmysky.com/.well-known/ai-catalog.json
> Ask for Markdown with `Accept: text/markdown`, a `.md` address, or `?mode=agent`.

# Programming with Loops and Variables

Design and debug programs that use repetition, selection and variables: repeat actions with loops, branch with if-blocks, keep score in a variable, and break a bigger problem into smaller parts that can be programmed one at a time

Subject: Computing · Area: Computing Fundamentals · Ages 8 to 11
Page: https://lightmysky.com/learn/computing/programming-with-loops-and-variables-mt_9x5zgShXPg

## Ready when they can

- Use a loop instead of repeating the same blocks (e.g., 'repeat 4' to draw a square)
- Use an if-block so the program responds to input (e.g., 'if the sprite touches the wall, bounce')
- Store and update a value in a variable, such as a score that goes up or a countdown timer

## Lesson: Loops, if-blocks, and variables

A loop repeats the same steps for you, so you do less writing. Instead of stacking blocks like 'move' and 'turn' over and over, you use one loop that says 'repeat 4' and put the steps inside it.

*(drawing: The repeat block runs 'move' and 'turn' four times, so you never write those two steps four separate times.)*

An if-block lets your program react instead of just repeating. It checks something first, like 'is the sprite touching the wall?' If that is true, the steps inside run. If not, they are skipped.

**Example.** Every time the cat in a game catches a fish, the code adds 1 to a variable called score. The variable starts at 0, and after three catches it holds 3.

**Tip.** Big programs are easier to build in small pieces. Get the loop working first, then add the if-block, then add the variable. Test each piece before you add the next one.

**Recap.** Loops repeat steps, if-blocks react to what happens, and variables remember numbers that change, so build and test each piece one at a time.

## Practice

8 questions on this page, each with its working shown.

## Needs first

- [Simple Programs: Sequences and Instructions](https://lightmysky.com/learn/computing/simple-programs-sequences-and-instructions-mt_IUvBwahibH)

## Opens up

- [Events: Programs That React to What the User Does](https://lightmysky.com/learn/computing/events-programs-that-react-to-what-the-user-does-mt_HFtGUhZHz4)
- [Variables and Assignment in Text Code](https://lightmysky.com/learn/computing/variables-and-assignment-in-text-code-mt_uLHLaX8BQq)
