7.7 Jump Statements

Jump statements cause an unconditional jump to another statement elsewhere in the code. They are used primarily to interrupt switch statements and loops.

The jump statements are the goto statement, the continue statement, the break statement, and the return statement, which are discussed in the following sections.

7.7.1 The goto Statement

The goto statement unconditionally transfers program control to a labeled statement, where the label identifier is in the scope of the function containing the goto statement. The labeled statement is the next statement executed. The goto statement has the following syntax:

goto identifier;

Care must be taken when branching into a block by using the goto statement, because storage is allocated for automatic variables declared within a block when the block is activated. When a goto statement branches into a block, automatic variables declared in the block are not initialized.

7.7.2 The continue Statement

The continue statement passes control to the end of the immediately enclosing while , do , or for statement. The continue statement has the following syntax:

continue;

The continue statement is equivalent to a goto statement within an iteration statement that passes control to the end of the loop body. For example, the following two loops are equivalent:

while(1)                           while(1)
{                                  {
   .                                  .
   .                                  .
   .                                  .
  goto label_1;                     continue;
   .                                  .
   .                                  .
   .                                  .
  label_1:
   ;                                  ;
 }                                  }

The continue statement can be used only in loops. A continue inside a switch statement that is inside a loop causes continued execution of the enclosing loop after exiting from the body of the switch statement.

7.7.3 The break Statement

The break statement terminates execution of the immediately enclosing while , do , for , or switch statement. Control passes to the statement following the loop body (or the compound statement of a switch statement). The break statement has the following syntax:

break;

See Example 7-1 which uses a break statement within a switch statement.

7.7.4 The return Statement

The return statement terminates execution of a function and returns control to the calling function, with or without a return value. A function may contain any number of return statements. The return statement has the following syntax:

return expression(opt);

If present, the expression is evaluated and its value is returned to the calling function. If necessary, its value is converted to the declared type of the containing function's return value.

A return statement with an expression cannot appear in a function whose return type is void . For more information about the void data type and function return types, see Sections 3.5 and 3.4.1.

If there is no expression and the function is not defined as void , the return value is undefined. For example, the following main function returns an unpredictable value to the operating system:

main ( )
  {
   return;
  }

Reaching the closing brace that terminates a function is equivalent to executing a return statement without an expression.


Previous Page | Next Page | Table of Contents | Index