Do-Loop
Noun · Development
Definitions
Do-Loop (or do-while loop) is a control flow statement that executes a block of code at least once and then repeatedly executes it as long as a specified condition remains true. Unlike a standard while loop, which checks the condition before the first execution (and may never execute the body), a do-while loop checks the condition after each execution, guaranteeing at least one iteration. The syntax in C, C++, Java, and JavaScript is: do { statements } while (condition). This construct is useful when the loop body must run at least once, such as prompting a user for valid input (display the prompt, then check if the input is acceptable) or processing data that needs at least one pass (read a record, then check if there are more). Python does not have a built-in do-while construct, but the pattern can be emulated with while True and a break condition. Some languages like Rust use loop with break for similar semantics. While less commonly used than for and while loops, do-while elegantly handles cases where the termination condition depends on the result of the loop body.
In plain English: A loop that always runs its code at least one time before deciding whether to keep going — act first, ask questions later.
Example: "Use a do-loop when you need the body to run at least once, like prompting for input until it's valid."
Origin Story
The Loop That Does Before It Asks
A do-loop (or do-while loop) is a control flow structure that executes its body at least once before checking a termination condition, guaranteeing a minimum of one iteration. The construct traces back to the earliest days of structured programming. FORTRAN, one of the first high-level programming languages (developed by John Backus and his team at IBM in 1957), included the DO statement as one of its core loop constructs. The FORTRAN DO loop specified a counter variable, a range, and a step size, iterating through the range at least once. As programming languages evolved, the do-while variant emerged, most notably in C (1972, Dennis Ritchie), where 'do { ... } while (condition);' became the standard syntax. The key distinction from a regular while loop is the placement of the condition check: a while loop might never execute its body if the condition is false initially, but a do-while always runs once. This makes it ideal for scenarios like input validation (prompt the user, then check if the input is valid) and menu-driven programs. While some modern languages like Python have omitted the do-while construct (Guido van Rossum considered it rarely necessary), it remains a fundamental control flow pattern in C, C++, Java, JavaScript, C#, and many other languages.
Coined by: John Backus and the IBM FORTRAN team
Context: The DO statement appeared in FORTRAN (1957); the do-while variant was formalized in C (1972).
Fun fact: Python's creator Guido van Rossum has repeatedly rejected proposals to add a do-while loop to Python, arguing that the existing 'while True: ... if condition: break' pattern is clear enough and that adding another loop type would violate the language's 'one obvious way to do it' philosophy.