Jump statements transfer control unconditionally.
goto identifier;
continue;
break;
return;
return(expression);
error(string-expression);
For the goto statement, identifier must be a labeled statement located in the current function. Control transfers directly to the labeled statement.
if (a < 0) goto label1;
b = 1;
c = 2;
label1:
d = 3;
The goto statement is useful for breaking out of deeply nested for or while loops:
a = 0;
for (j = 1; j < n; j++)
{
for (k = 1; k < j; k++)
{
for (l = 1; l < k; l++)
{
a += j * k * l;
if (a > n) goto exitpt;
}
}
}
exitpt:
return(a);
The break statement terminates the immediately enclosing for or while loop.
while (a < n)
{
a += a;
if (a <= 0) break;
}
for (j = 1; j < n; j++)
{
for (k = 1; k < j; k++)
{
a += k;
if (a > n) break;
}
}
In the second example, the break statement terminates the inner for loop and returns control to the outer for loop.
The continue statement terminates execution of any remaining statements in the body of a for or while loop. Control is transferred to the end of the body and the next loop test expression is evaluated.
for (j = 1; j < n; j++)
{
if (a > 0) continue;
a += j;
}
The return statement is used to terminate the current iteration or function and optionally return a value. If one or more values are returned, they must be enclosed in ( ).
return;
return(a+10);
return(x, x*2);
The error statement aborts further function processing and displays an optional message.
invnum(x)
{
if (x == 0)
{
error("invnum - input must be non-zero");
}
else
{
return(1/x);
}
}
invnum(0) aborts and displays:
invnum - input must be non-zero
sqrt(invnum(0)) aborts and displays:
invnum - input must be non-zero
As indicated by the second example, nested functions that execute the error function quit processing.