While Loop

Also known as: while, condition-controlled loop

definition

A control structure that repeats a block of code as long as a specified condition remains true. While loops handle situations where you don't know in advance how many iterations are needed.

๐Ÿ’ก Intuition

A while loop is like 'keep going until...' โ€” keep stirring the soup WHILE it's not boiling. Keep asking for a password WHILE the password is wrong.

Core Idea

While loops check the condition before each iteration. If the condition is false from the start, the body never executes.

๐Ÿ”ฌ Example

count = 0. WHILE count < 5: PRINT count. count = count + 1. This prints 0, 1, 2, 3, 4.

๐ŸŽฏ Why It Matters

While loops handle situations where you don't know in advance how many iterations are needed.

โš ๏ธ Common Confusion

Forgetting to update the loop variable creates an infinite loop. Always make sure the condition will eventually become false.

Related Concepts

Prerequisites

Next Steps

How While Loop Connects to Other Ideas

To understand while loop, you should first be comfortable with iteration. Once you have a solid grasp of while loop, you can move on to for loop.

Go Deeper

Frequently Asked Questions

What is While Loop in CS Thinking?

A control structure that repeats a block of code as long as a specified condition remains true.

Why is While Loop important?

While loops handle situations where you don't know in advance how many iterations are needed.

What do students usually get wrong about While Loop?

Forgetting to update the loop variable creates an infinite loop. Always make sure the condition will eventually become false.

What should I learn before While Loop?

Before studying While Loop, you should understand: iteration.