liveBook · Manning

Do statement

As explained in Modern C, Third Edition

A do-while loop is a C iteration statement that executes its body once and then repeats it while its controlling condition is true.

A do statement repeats a body while its controlling condition is true, but tests that condition after the body runs. Therefore, the body executes at least once. section 3.0 section 3.2

while (condition) secondary-block
do secondary-block while(condition);

Compare the preconditioned while form with the postconditioned do form: do places the condition after the secondary block and ends the statement with a semicolon. section 3.2

Repeat an approximation until it is close enough

do { x *= (2.0 - a*x); } while (fabs(1.0 - a*x) >= ε);

This form suits an operation that must happen before its stopping test. In the approximation, each iteration updates x, then checks whether a*x is close enough to 1.0.

TAKEAWAY rules:

  • Use do when the first execution is required; use while when the condition must be checked before entering the body. section 3.2
  • Treat 0 as false and every other numerical value as true when evaluating the controlling condition. section 3.4
  • Prefer structured control flow, while recognizing that loops add control-flow paths a reader must track. section 19.0

How Modern C, Third Edition covers it

How deep it goes
Defines syntax and execution order, demonstrates a numerical loop, and stops short of deeper control-flow mechanics.
Examples
Demonstrates Heron's approximation for a multiplicative inverse using a precision threshold and repeated updates.
What it assumes
Assumes familiarity with statements and conditions while distinguishing do from the already encountered for and while forms.
What you can do after
Apply it via C syntax to repeat a body at least once and terminate after a condition becomes false.
How it connects
Contrasts do with while and for, and places both within structured control flow alongside if and switch.

Also explained in

Compare how 2 books explain do statement →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.