A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

While Loops

College Depth 73 in the knowledge graph ☐ I know this ☆ Set as goal
685topics build on this
323prerequisites beneath it
See this on the map →
Comparison Operators and Boolean TestsConditional Statements+2 more→→Do-While Loops and Post-Test IterationFor Loops+4 more
while iteration loops control flow termination

Core Idea

A while loop repeatedly executes a block of code as long as its condition remains true. Before each iteration the condition is evaluated; when it becomes false the loop exits and execution continues after the loop body. The loop body must eventually cause the condition to become false, or the loop runs forever (an infinite loop). While loops are best used when the number of iterations is not known in advance.

How It's Best Learned

Trace loops by hand, updating a variable table after each iteration. Deliberately create an infinite loop, observe the behavior, and fix it. Implement classic examples: countdown, sum of digits, user input validation.

Common Misconceptions

Explainer

You already understand conditional statements — the idea that a program can check a condition and decide what to do. A while loop extends this idea by adding repetition: instead of checking the condition once, the program checks it *before every iteration* and keeps executing the loop body as long as the condition remains true. The structure is `while (condition) { body }`. The computer evaluates the condition; if true, it runs the body, then goes back and evaluates the condition again. This cycle repeats until the condition is false, at which point the loop exits and the program continues with whatever comes after.

The critical design requirement is that something inside the loop body must eventually make the condition false. If nothing changes the variables involved in the condition, the condition stays true forever and the loop never exits — this is an infinite loop, and it is one of the most common bugs beginners encounter. Typically, the loop body updates a counter, advances through a data structure, or receives new input. For example, in a countdown loop, you decrement a counter each iteration: `while (count > 0) { print(count); count = count - 1; }`. The counter decreases each time, eventually reaching zero, at which point `count > 0` is false and the loop ends.

While loops are the right tool when you do not know in advance how many iterations you need. Reading user input until they type "quit," searching a list until you find a match, or repeatedly halving a number until it drops below a threshold — in all these cases, the number of repetitions depends on data you do not have until the program is running. This distinguishes while loops from for loops (which you will learn next), where the iteration count is typically known or bounded at the start.

One subtle point: the condition is checked only at the top of each iteration, not continuously during the body. If the condition becomes false partway through the body, the rest of the body still finishes executing before the loop checks again. This means a while loop always completes its current iteration in full. Understanding this timing is essential for avoiding off-by-one errors — the most common mistake is using `<` when you meant `<=` (or vice versa), causing the loop to run one too many or one too few times. When in doubt, trace through the first two and last two iterations by hand, tracking every variable, to verify your loop does exactly what you intend.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → While Loops

Longest path: 74 steps · 323 total prerequisite topics

Prerequisites (4)

Leads To (6)