Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The Java programming language supports three branching statements:The
break
statement and the continue statement, which are covered
next, can be used with or without a label. A label is an identifier
placed before a statement. The label is followed by a colon (:
):
You'll see an example of a label within the context of a program in the next section.statementName: someJavaStatement;
The break statement has two forms: unlabeled and labeled. You saw the unlabeled form of thebreak
statement used withswitch
earlier. As noted there, an unlabeledbreak
terminates the enclosingswitch
statement, and flow of control transfers to the statement immediately following theswitch
. You can also use the unlabeled form of thebreak
statement to terminate afor
,while
, ordo-while
loop. The following sample program,BreakDemo
, contains afor
loop that searches for a particular value within an array:Thepublic class BreakDemo { public static void main(String[] args) { int[] arrayOfInts = { 32, 87, 3, 589, 12, 1076, 2000, 8, 622, 127 }; int searchfor = 12; int i = 0; boolean foundIt = false; for ( ; i < arrayOfInts.length; i++) { if (arrayOfInts[i] == searchfor) { foundIt = true; break; } } if (foundIt) { System.out.println("Found " + searchfor + " at index " + i); } else { System.out.println(searchfor + "not in the array"); } } }break
statement terminates thefor
loop when the value is found. The flow of control transfers to the statement following the enclosingfor
, which is the print statement at the end of the program.The output of this program is:
The unlabeled form of the break statement is used to terminate the innermostFound 12 at index 4switch
,for
,while
, ordo-while
; the labeled form terminates an outer statement, which is identified by the label specified in thebreak
statement. The following program,BreakWithLabelDemo
, is similar to the previous one, but it searches for a value in a two-dimensional array. Two nestedfor
loops traverse the array. When the value is found, a labeledbreak
terminates the statement labeledsearch
, which is the outerfor
loop:The output of this program is:public class BreakWithLabelDemo { public static void main(String[] args) { int[][] arrayOfInts = { { 32, 87, 3, 589 }, { 12, 1076, 2000, 8 }, { 622, 127, 77, 955 } }; int searchfor = 12; int i = 0; int j = 0; boolean foundIt = false; search: for ( ; i < arrayOfInts.length; i++) { for (j = 0; j < arrayOfInts[i].length; j++) { if (arrayOfInts[i][j] == searchfor) { foundIt = true; break search; } } } if (foundIt) { System.out.println("Found " + searchfor + " at " + i + ", " + j); } else { System.out.println(searchfor + "not in the array"); } } }This syntax can be a little confusing. TheFound 12 at 1, 0break
statement terminates the labeled statement; it does not transfer the flow of control to the label. The flow of control transfers to the statement immediately following the labeled (terminated) statement.
continue
StatementYou use the continue statement to skip the current iteration of afor
,while
, ordo-while
loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop, basically skipping the remainder of this iteration of the loop. The following program,ContinueDemo
, steps through a string buffer checking each letter. If the current character is not ap
, thecontinue
statement skips the rest of the loop and proceeds to the next character. If it is ap
, the program increments a counter, and converts thep
to an uppercase letter.Here is the output of this program:public class ContinueDemo { public static void main(String[] args) { StringBuffer searchMe = new StringBuffer( "peter piper picked a peck of pickled peppers"); int max = searchMe.length(); int numPs = 0; for (int i = 0; i < max; i++) { //interested only in p's if (searchMe.charAt(i) != 'p') continue; //process p's numPs++; searchMe.setCharAt(i, 'P'); } System.out.println("Found " + numPs + " p's in the string."); System.out.println(searchMe); } }The labeled form of theFound 9 p's in the string. Peter PiPer Picked a Peck of Pickled PePPerscontinue
statement skips the current iteration of an outer loop marked with the given label. The following example program,ContinueWithLabelDemo
, uses nested loops to search for a substring within another string. Two nested loops are required: one to iterate over the substring and one to iterate over the string being searched. This program uses the labeled form ofcontinue
to skip an iteration in the outer loop:Here is the output from this program:public class ContinueWithLabelDemo { public static void main(String[] args) { String searchMe = "Look for a substring in me"; String substring = "sub"; boolean foundIt = false; int max = searchMe.length() - substring.length(); test: for (int i = 0; i <= max; i++) { int n = substring.length(); int j = i; int k = 0; while (n-- != 0) { if (searchMe.charAt(j++) != substring.charAt(k++)) { continue test; } } foundIt = true; break test; } System.out.println(foundIt ? "Found it" : "Didn't find it"); } }Found it
return
StatementThe last of Java's branching statements is the return statement. You usereturn
to exit from the current method. The flow of control returns to the statement that follows the original method call. Thereturn
statement has two forms: one that returns a value and one that doesn't. To return a value, simply put the value (or an expression that calculates the value) after thereturn
keyword:The data type of the value returned byreturn ++count;return
must match the type of the method's declared return value. When a method is declaredvoid
, use the form ofreturn
that doesn't return a value:return;
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.