liveBook · Manning

Break statement

As explained in Modern C, Third Edition

A break statement terminates the nearest enclosing switch or iteration statement and transfers control to the following statement.

A break statement exits the nearest enclosing iteration or switch statement immediately. In a loop, it skips termination-condition reevaluation and the remaining statements in the current iteration; in a switch, it transfers control to the statement after the switch. section 3.2 section 3.3

Stopping an iterative approximation

while (true) { double prod = a*x; if (fabs(1.0 - prod) < ε) { break; } x *= (2.0 - prod); } The loop computes the product, tests whether the approximation is close enough, and exits without performing another update when the test succeeds. section 3.2

Use break deliberately in these control-flow contexts:

  • Place it inside a loop when a condition discovered during the body determines that iteration should stop. section 3.2
  • Place it in a switch case when execution should not fall through into subsequent case statements. section 3.3 section 3.4
  • Distinguish it from continue: continue skips the rest of the current loop body but reevaluates the loop condition. section 3.2
while (true) {
  double prod = a*x;
  if (fabs(1.0 - prod) < ε) {       // Stops if close enough
    break;
  }
  x *= (2.0 - prod);                // Heron approximation
}

View a compact loop example that separates computation, stopping-condition evaluation, and the update controlled by break. section 3.2

How Modern C, Third Edition covers it

How deep it goes
Explains loop and switch control flow with examples, while stopping short of broader execution mechanics.
Examples
Uses a Heron approximation loop, multiplicative-inverse program, and corvid switch examples.
What it assumes
Assumes familiarity with C control expressions, compound statements, loops, and switch syntax.
What you can do after
Apply break in C loops and switch statements to implement conditional termination and prevent unintended fall-through.
How it connects
Connects break with iteration statements, switch statements, continue statements, case labels, and fall-through.

Also explained in

Compare how 7 books explain break 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.