Java Reference
In-Depth Information
OntheWeb
TheprogramContinueDemo.javaisfoundintheChapter11folderat
www.crcpress.com .
// File name: ContinueDemo.java
// Reference: Chapter 11
//
// Java program to demonstrate direct flow control
// Topics:
//
1. Action of the continue statement
//
public class ContinueDemo
{
public static void main(String[] args)
{
char letter;
for (letter = 'A'; letter < 'E'; letter ++)
{
if (letter == 'C')
continue;
System.out.println(" letter is " + letter);
}
}
}
The labeled break
Abreakstatementisusedtoexitthecurrentlevelofaloop.Occasionally,
anapplicationneedstoimmediatelyexitalllevelsinaloop;forexample,if
anerrorisdetected.Onepossiblesolutionistoincludeanifstatementin
theloopandaddanadditionalterminatingconditiontotheloopheader.
Butwhendealingwithseveralnestinglevelstheseextraconditionscanbe
inconvenientandcomplicated.
TheJavainstructionthatprovidesanimmediateandunconditional
exitforanylevelofanestedloopisthelabeledbreak.Inthiscase,the
breakstatementisfollowedbytheidentifierofaprogramlabel.Thelabel
itselfisaplace-markerendinginthecolonsymbol.Thegeneralformof
thelabeledbreakisshownin Figure11-2 .
Programmers note:
The label must precede the outermost loop level which you want to
exit.
Search WWH ::




Custom Search