Java Reference
In-Depth Information
Direct Flow Control
Theloopstatementsdiscussedhereandin Chapter10 ,aswellasthedeci-
sionstatementsin Chapter9 ,providetheprogrammerwithwaysofdirect-
ing program execution. In loops and in the various flavors of the if
statement, program execution flows according to the result of the test ex-
pression that evaluates to true or false. Because their action depends on the
result of a logical expression, it is said that loops and decision statements
indirectly change the flow of a program.
But occasionally you may need to change program execution immedi-
ately and without performing any test. Java contains instructions that
abruptly change execution. These instructions are break, continue, and
return. The return statement is associated with methods, therefore it is
discussedin Chapter12 .
Incidentally...
C and C++ contain an additional statement, named goto, that allows
directing execution unconditionally to a specific destination. Al-
though goto is a Java reserved word, the goto statement has not been
implemented in the language.
Using break in loops
In Chapter 9 we saw the use of break in a switch construct to terminate
the processing action in a case block. The break statement can also be
used in a for, while, or do-while loop. In the case of loops, the break state-
ment directs execution to the line immediately after the currently execut-
ing level of the loop. When break is used in a nested loop, execution exits
the loop level in which the statement is located. This is shown by the pro-
gram named BreakDemo.java listed below.
On the Web
The program BreakDemo.java is found in the Chapter 11 folder at
www.crcpress.com .
//
File name: BreakDemo.java
//
Reference: Chapter 11
//
//
Java program to demonstrate direct flow control
//
Topics:
//
1. Action of the break statement
//
Search WWH ::




Custom Search