---
title: "Condition-Controlled Loops"
description: "Repeating for as long as a condition stays true, for when the number of repeats is not known in advance, and making sure something inside the loop can make the condition false."
canonical: https://lightmysky.com/learn/computing/condition-controlled-loops-mt_LjxO2n3Fy6
source: https://lightmysky.com/learn/computing/condition-controlled-loops-mt_LjxO2n3Fy6.md
retrieved: 2026-09-12
---

> **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`.

# Condition-Controlled Loops

Repeating for as long as a condition stays true, for when the number of repeats is not known in advance, and making sure something inside the loop can make the condition false.

Subject: Computing · Area: Programming · Ages 15 to 16
Page: https://lightmysky.com/learn/computing/condition-controlled-loops-mt_LjxO2n3Fy6

## Ready when they can

- Write a loop that keeps asking until the input is valid
- Point at the line that lets the loop end, and say what happens without it
- Choose between a counted loop and a condition-controlled loop for a described task

## Lesson: Looping until the answer is right

A while loop is a condition-controlled loop. It keeps repeating its block for as long as its condition stays True, and stops the moment it turns False. A for loop is different: it repeats a set number of times, or once for each item in a list. Pick while when you do not know ahead of time how many repeats you need.

The computer checks the condition, runs the body once if it is True, then checks again. If it is False, the loop ends and the program moves on. Any True or False expression works as the condition, including compound ones built with and and or. Because the check repeats every pass, something inside the body must be able to change it.

**Example.** To keep asking until the right password is typed, set keep_asking to True and loop while it holds. Inside, ask for an entry, and set keep_asking to False when the entry matches. The ask line is what lets the loop end. Without it the condition could never flip, and the loop would run forever.

**Tip.** Choose by counting what you know. A class list of known size calls for a for loop. Waiting for a signal, a password or a sensor reading calls for while. Before you run it, point at the line inside that can flip the condition and ask what happens without it.

**Recap.** While repeats until its condition flips, so the body must hold the line that flips it.

## Practice

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

## Needs first

- [Count-Controlled Loops](https://lightmysky.com/learn/computing/count-controlled-loops-mt_r1eXTWI_6j)

## Opens up

- [Strings: Indexing, Slicing and Length](https://lightmysky.com/learn/computing/strings-indexing-slicing-and-length-mt_33hA050_Qz)
