Fiveable
Fiveable

while loop

Definition

A while loop is a control flow statement that allows a block of code to be executed repeatedly as long as a specified condition is true.

Analogy

Imagine you're playing a video game and your character needs to keep collecting coins until they reach a certain score. The while loop is like the game's instructions telling your character to keep collecting coins until the score reaches the desired value.

Related terms

Condition: A condition is an expression that evaluates to either true or false, determining whether the code inside the while loop should be executed.

Loop body: The loop body refers to the block of code that gets executed repeatedly as long as the condition in the while loop remains true.

Infinite loop: An infinite loop occurs when the condition in a while loop always evaluates to true, causing the code inside it to repeat indefinitely.



© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.