next up previous
Next: Example of elimination of Up: Unit 06 Previous: Use of the break

Elimination of break

The execution of a break statement modifies the execution flow. Hence, when it is used in loops:

In general, it is always possible to eliminate a break statement. For example, the statement

while (condition) {
  statements-1
  if (break-condition) break;
  statements-2
}

is equivalent to

boolean finished = false;
while (condition && !finished) {
  statements-1
  if (break-condition)
    finished = true;
  else {
    statements-2
  }
}

The choice on whether to eliminate or not a break must be made by evaluating:


next up previous
Next: Example of elimination of Up: Unit 06 Previous: Use of the break