Flow Control and Exception
Handling
Write
code using all forms of loops including labeled and
unlabeled use of break and continue, and state the
values taken by loop control variables during and
after loop execution.
The do-while loop
This
loop is especially useful when you process a menu
selection, because you will usually want the body of a
menu loop to execute at least once.
The for loop variations
The
three sections of the for
loop can be used for any purpose you desire.
-
One of the most common variations
involves the conditional expression. Specifically,
this expression does not need to test the loop
control variable against some target value. In fact,
the condition controlling the
for loop can be any
boolean expression.
-
Either the initialization or the
iteration expression or both can be absent. There
can be times when this type of approach makes sense.
For example, if the initial condition is set through
a complex expression elsewhere in the program or if
the loop control variable changes in a
non-sequential manner determined by actions that
occur within the body of the loop, it may be
appropriate to leave these parts of the
for loop empty.
-
You can intentionally create an
infinite loop if you leave all three parts of the
for loop empty.
Because, then there is no condition under which it
will terminate.
Although there are some programs, such as operating
system command processors, that require an infinite
loop, most infinite loops are really just loops with
special termination requirements.
The break statement
It
has three uses:
By using
break, you can force
immediate termination of a loop, bypassing the
conditional expression and any remaining code in the
body of the loop. When a break
is encountered inside a loop, the loop is terminated
and program control resumes at the next statement
following the loop. When used inside a set of nested
loops, the break
statement will only break out of the innermost loop.
Two more points to remember: first, more than one
break statements can
appear in a loop. But, too many
break statements have the tendency to
de-structure your code. Second, the
break that terminates a
switch statement, affects
only that switch
statement and not any enclosing loops.
This
statement was not developed to provide the normal
means by which a loop is terminated. The loop’s
conditional expression serves that purpose. The
break statement should be
used to cancel a loop only in special cases.
The
break statement cannot be
used to break out of an if
statement.
Using break as a form of goto
Java
does not support goto statement, because it provides a
way to branch in an arbitrary and unstructured manner.
This usually makes goto-ridden code hard to understand
and hard to maintain. It also prohibits certain
compiler optimizations. There are, however, a few
places where goto is a valuable and legitimate
construct for flow control. For example, the goto can
be useful when you are exiting from a deeply nested
set of loops. To handle such situations, Java provides
labeled break statement.
By using this you can break out of the one or more
blocks of code. These blocks need not be part of a
switch or a loop.
Further, you can specify precisely where execution
will resume, because this form of
break works with a label.
When
this form of break
executes, control is transferred out of the named
block of the code. The labeled block of code must
enclose the break
statement, but it need not be the immediately
enclosing block. This means that you can use a labeled
break statement to exit
from a set of nested blocks. But you cannot use
break to transfer control
to a block of code that does not enclose a
break statement.
A
label is any valid Java identifier followed by a
colon. Once you have labeled a block, you can then use
this label as the target of a
break statement. Doing so causes execution to
resume at the end of the labeled block.
Keep
in mind that you cannot break to any label which is
not defined for an enclosing block i.e. if the labeled
block/loop does not enclose a
break statement, it is not possible to transfer
control to that block.
The continue statement:
Sometimes it is useful to force an early iteration of
a loop. You might want to continue running the loop,
but stop processing the remainder of the code in its
body for this particular iteration. In
while and
do-while
loops continue statement
causes control to be transferred directly to the
conditional statement that controls the loop. In a
for loop, control first
goes to the iteration portion of the
for statement and then to
the conditional expression. For all three loops, any
intermediate code is bypassed.
As
with the break statement,
continue may specify a
label to describe which enclosing loop to continue.
Good
uses to continue are
rare. One reason is that Java provides a rich set of
loop statements which fit most applications. However,
for those special circumstances in which early
iteration is needed, the
continue statement provides a structured way to
accomplish it.
The return statement:
This
is explicitly used to return from a method. It causes
program control to transfer back to the caller of the
method. The return
statement immediately terminates the method in which
it is executed.
class ReturnTest{
public
static
void main (String []
args){
boolean t= true;
System.out.println (“Before the return.”);
if (t)
return;
System.out.println (“This will not execute.”);
}
}
In
this program, the if (t)
statement is necessary. Without it, the java compiler
would flag an “unreachable code” error, because the
compiler would know that the last println() statement
would never be executed. To prevent this error, the
if statement is used here
to trick the compiler for the sake of this
demonstration.
In
addition to these jump statements, Java supports one
other way that you can change your program’s flow of
execution: through exception handling.
section2-1 | section2-2 | section2-3
Sections :
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11
|