Java Reference
In-Depth Information
reviewing Keywords for control
Chapter 2 briefly covered Java keywords in a more general way. This section reviews keywords used
in control structures. Many of these keywords have already been featured in this chapter, as they
are specific to the types of control structures you've been reading about. Table 5.4 lists the control
keywords and their associated control structures. This section focuses on break , continue , and
return , which are more general and can be found in different kinds of structures.
table 5.4: Control Keywords
Key Word
associated control structure
for loop
Enhanced for loop
for
while loop
do while loop
while
do while loop
do
if-then statement
if
if-then statement
else
switch statement
switch
switch statement
case
switch statement
default
General use
break
General use
continue
General use
return
The last three keywords— break , continue , and return —all interrupt the execution of the current
block of code. The differences among them is found in what happens after the interruption.
Controlling with the return Keyword
You have seen the return keyword as part of a return statement already, so it may seem strange to
think of it as part of a control structure. You know that a return statement of a method completes
the execution of that method by returning a value. So, whenever a return statement is executed,
that method is stopped, regardless of where in the list of statements it is placed. To imagine how this
can be used for control, take the following example:
//array of employee ID numbers, stored as Strings
static String[] employees;
//method to search for a specified employee ID
static boolean findEmployee(String employeeID){
for (String emp : employees){
 
Search WWH ::




Custom Search