Skip to content

2.3.1 The while Loop

The while loop repeats a block of code as long as a condition remains True.
It is commonly used when the number of iterations is not known in advance.


How the while Loop Works

  • The condition is evaluated before each iteration
  • If the condition is True, the loop body executes
  • If the condition becomes False, the loop stops

Common Use Cases

  • Repeating actions until a condition is met
  • Waiting for a specific state
  • Implementing input validation loops

📌 Note:
Always ensure that the loop condition can eventually become False to avoid infinite loops.