What is while?

While is like having a toy that keeps playing until something changes, you get to do something fun while it's still going on.

Imagine you have a favorite spinning top. You keep spinning it while it’s still turning. As soon as it stops, you stop too. That’s what while does in coding: it lets the computer do something over and over while a certain condition is true.

How It Works

Think of it like this: you're playing with your friend while the game is on. As long as the game is still happening, you keep playing. But when the game ends, maybe someone loses or the timer runs out, you stop.

In code, it looks a bit like:

while the top is still spinning:
 keep spinning

So the computer checks if something is true, like "is the top still spinning?", and keeps doing the action while that's still true. Once the condition becomes false, the top stops, the computer moves on to the next part of the code.

It’s like having a rule: “Do this fun thing while it still makes sense.”While is like having a toy that keeps playing until something changes, you get to do something fun while it's still going on.

Imagine you have a favorite spinning top. You keep spinning it while it’s still turning. As soon as it stops, you stop too. That’s what while does in coding: it lets the computer do something over and over while a certain condition is true.

Take the quiz →

Examples

  1. A 'while' loop is like telling a robot to keep doing something until a condition changes, like counting from 1 to 10.
  2. Imagine you're waiting for the bus and continue waiting until it arrives, that's like a while loop.
  3. A 'while' loop helps repeat code as long as a condition is true.

Ask a question

See also

Discussion

Recent activity

Categories: Culture · loops· programming· while loop