← All lectures

Lecture 10 · 25 min

while Loop

The while loop — a pretest, condition-controlled loop that runs zero or more times based on a test expression.

while syntaxPretest loopCounter & sentinelIncrement operators
Slide 1 / 14

Why use `while` instead of `for`?

The for loop is great when you know in advance how many times to repeat.

But what about: "Keep asking the user for a number until they enter 0"? You don't know how many times they'll enter numbers!

The `while` loop is designed for exactly this situation — it repeats as long as a condition is true, without knowing in advance how many iterations are needed.

← / → navigateF fullscreenEsc exit⌘K search