Google

Go to the first, previous, next, last section, table of contents.


loop, break, return, continue

There are three kinds of statements for loops (repetitions): the while statement, the for statement, and the do statement.

  • while statement
    It has the following form.
    while ( expression ) statement
    
    This statement specifies that statement is repeatedly evaluated as far as the expression evaluates to a non-zero value. If the expression 1 is given to the expression, it forms an infinite loop.
  • for statement
    It has the following form.
    for ( expression list-1; expression; expression list-2 ) statement
    
    This is equivalent to the program
    expression list-1 (transformed into a sequence of simple statement)
    while ( expression ) {
        statement
        expression list-2 (transformed into a sequence of simple statement)
    }    
    
  • do statement
    do { 
        statement
    } while ( expression )
    
    This statement differs from while statement by the location of the termination condition: This statement first execute the statement and then check the condition, whereas while statement does it in the reverse order.

As means for exiting from loops, there are break statement and return statement. The continue statement allows to move the control to a certain point of the loop.

  • break
    The break statement is used to exit the inner most loop.
  • return
    The return statement is usually used to exit from a function call and it is also effective in a loop.
  • continue
    The continue statement is used to move the control to the end point of the loop body. For example, the last expression list will be evaluated in a for statement, and the termination condition will be evaluated in a while statement.


Go to the first, previous, next, last section, table of contents.