![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The Java programming language supports three branching statements:
Thebreak
statement and thecontinue
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 breakstatement has two forms: unlabelled and labelled. You saw the
break
statement in action within theswitch
statement earlier. As noted there,break
terminates enclosing theswitch
statement and flow of control transfers to the statement immediately following theswitch
. You can also use the unlabelled form of thebreak
statement to terminate afor
,while
, ordo-while
loop. The following sample program,BreakDemo
, contains a
for
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:
While, the unlabelled form of theFound 12 at index 4break
statement is used to terminate the innermostswitch
,for
,while
, ordo-while
, the labelled form terminates an outer statement, which is identified by the label specified in thebreak
statement. The following program,BreakWithLabelDemo
, is similar to the previous, but it searches for a value in a two-dimensional array. Two nested
for
loops traverse the array. When the value is found, a labelled break terminates the statement labelledsearchfor
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 labelled statement; it does not transfer the flow of control to the label. The flow of control transfers to the statement immediately following the labelled (terminated) statement.
You use the continuestatement to skip the current iteration of a
for
,while
, ordo-while
loop. The unlabelled 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 a `p', the
continue
statement skips the rest of the loop and proceeds to the next character. If it is a `p'. the program increments a counter, and converts the `p' to an uppercase letterHere 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 labelled 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 labelled form of
continue
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
The last of Java's branching statements is the returnstatement. You use
return
to exit from the current method and jump back to the statement within the calling method that follows the original method call. There are two forms ofreturn
: 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 value returned byreturn ++count;return
must match the type of method's declared return value.When a method is declared
void
use the form ofreturn
that doesn't return a value:return;
![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |