While Loops

👨‍💼 Great! While loops are perfect when the number of iterations depends on some condition.
🦉 There's also a do...while loop that runs at least once:
let input
do {
	input = getInput() // Always runs at least once
} while (input !== 'quit')
When to use each:
LoopUse When
forYou know exactly how many iterations
whileYou don't know how many iterations, check before each
do...whileYou need at least one iteration, check after each
The break keyword immediately exits any loop:
while (true) {
	if (foundIt) break // Exit the loop
}
The continue keyword skips to the next iteration:
for (let i = 0; i < 10; i++) {
	if (i === 5) continue // Skip 5
	console.log(i)
}

Please set the playground first

Loading "While Loops"
Loading "While Loops"