Information Technology Reference
In-Depth Information
Jump Statements
When the flow of control reaches
jump statements
, program execution is unconditionally
transferred to another part of the program. The jump statements are the following:
break
continue
return
goto
throw
This chapter covers the first four of these statements. The
throw
statement is discussed in
Chapter 11.
The break Statement
You have already seen the
break
statement earlier in this chapter—used in the
switch
state-
ment. But it can also be used in the following statement types as well:
for
foreach
while
do
switch
In the body of one of these statements,
break
causes execution to exit the
innermost
enclosing statement
.
For example, the following
while
loop would be an infinite loop if it relied only on its test
expression, which is always
true
. But instead, after three iterations of the loop, the
break
state-
ment is encountered and the loop is exited.
int x = 0;
while( true )
{
x++;
if( x >= 3 )
break;
}