Repeats a block of statements based on a condition.
Details
A while statement repeats a block of statements while a boolean condition is true.
🤖 Pseudocode
while condition is true
repeat
🤖 Example: Loop Flow
preheat oven
mix ingredients
if chocolate cake:
add cocoa powder
while batter is lumpy:
stir batter
pour batter into pan
bake
decorate
It’s important to avoid creating an infinite loop - a loop that never ends because the condition is always true.
When you create an infinite loop, the program execution flow gets stuck inside the loop and won’t execute anything else.
🤖 Pseudocode
energy = 100
while energy > 0
player runs
If the energy value don’t decrease, the loop runs forever.
To prevent it, you need to make sure, that the condition value is somehow changed to false, while the loop is running.
🤖 Pseudocode
energy = 100
while energy > 0
player runs
energy - 1
Related Concepts
Related Actions
- Is created: Write a While Statement
- Used in: Break a While Loop