Repeating code while a condition is true
While loops execute a block of code repeatedly as long as a specified condition remains True. The condition is checked before each iteration, and if it evaluates to True, the loop body executes. While loops are useful when you don't know in advance how many iterations are needed, such as when processing input until a sentinel value is encountered, implementing game loops, or waiting for a condition to change. It's crucial to ensure the condition eventually becomes False to avoid infinite loops. This typically involves modifying variables used in the condition within the loop body. While loops can be combined with break statements to exit based on conditions within the loop body. They are more flexible than for loops for situations where the number of iterations isn't predetermined, but they require careful construction to ensure termination and avoid off-by-one errors. While loops are also used for polling scenarios and event loops in various applications.