Statements: break

The break statement ends the for, while or do-while loop immediately.

Syntax

break

Discussion

The break statement, when executed inside a loop, causes the loop to exit immediately passing the control to the next statement following the loop. Following examples demonstrate the usage of the break statement:

// Within a 'while' loop
while ( inx++ < count )
{
  var int32 v = Array[ inx ];

  if ( !v )
    break;

  sum += math_pow( 2, v );
}

// Within a 'do-while' loop
do
{
  var int32 v = Array[ inx ];

  if ( !v )
    break;

  sum += math_pow( 2, v );
}
while ( inx++ < count );

// Within a 'for' loop
for ( inx = 0; inx < count; inx++ )
{
  var int32 v = Array[ inx ];

  if ( !v )
    break;

  sum += math_pow( 2, v );
}

In case of nested loops, the break statement affects the nearest enclosing loop in which it appears. Please note, break statements are intended to be used within the body of a loop only. The usage of break outside a loop causes Chora compiler error message.