Skip to main content

What is a post-condition loop?

Short answer

A post-condition loop is a loop in which the condition is checked after the loop body. That is why the body executes at least once. The classic example is the do...while construct in languages of the C/Java/JS family.

Detailed breakdown

Definition and key properties

  • The condition is checked after the loop body executes.
  • At least one iteration is guaranteed to run, even if the condition is false from the start.
  • Used when an action needs to run at least once (for example, showing a menu, prompting for input, and so on).
  • The typical form in C-like languages: do { ... } while (condition);

Syntax and examples

JavaScript / Java / C / C++: do...while

javascript
let i = 10; do { console.log("Ran at least once: i =", i); i++; } while (i < 0); // The condition is false from the very start (10 < 0 - false), // but the body ran once.

A practical example: asking the user for a number in the range 1-10. The body runs at least once to make the first prompt.

javascript
let num; do { const input = prompt("Enter a number from 1 to 10:"); num = Number(input); } while (Number.isNaN(num) || num < 1 || num > 10); console.log("You entered:", num);

Python: an equivalent via while True + break

Python has no built-in do...while, but the behavior of a post-condition loop is easy to express with an infinite loop and an explicit break after checking the condition.

python
while True: num = int(input("Enter a number from 1 to 10: ")) if 1 <= num <= 10: break print("You entered:", num)

How does it differ from a pre-condition loop (while)?

  • while: the condition is checked before the body runs. If the condition is false, the body may never run.
  • do...while: the condition is checked after the body runs. The body will run at least once.

An equivalent transformation

A post-condition loop can be rewritten as a pre-condition loop, but only by duplicating the body once before the while:

text
do { // body } while (cond); // Equivalent to // body while (cond) { // body }

Typical usage scenarios

  • User input/menus: showing a form/menu at least once.
  • Retrying an attempt: at least one attempt at an operation (for example, an API request) with retries on failure up to N times.
  • Initialization that depends on the result of the first iteration: perform the action first, then decide whether to continue.

Important nuances and pitfalls

  • Risk of an infinite loop: make sure the state that affects the condition changes inside the body.
  • Side effects in the condition: avoid complex logic in the while condition; it is better to compute a flag beforehand.
  • Syntax in C-like languages: a trailing ';' is required after while (cond).
  • Initialization: remember that the first iteration runs before the check - prepare the input data/state correctly.

Briefly for an interview

  • Definition: a loop that checks the condition after the body (do...while).
  • Guarantee: at least one iteration.
  • Comparison: unlike while/for, the check happens at the end.
  • Usage: input/menus/retrying operations until a valid result.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.