Break statement
The break statement has two forms: Label and non-label. In the previous switch statement, the break statement is not in the label format. You can use non-label break to end the for, while, do-while loop, as shown in the following breakdemo.Program:
Class breakdemo {public static void main (string [] ARGs) {int [] arrayofints = {32, 87, 3,589, 12,107 6, 2000, 8,622,127}; int searchfor = 12; int I; Boolean foundit = false; for (I = 0; 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 ");}}}
This program finds the number 12 at the end of the array. The break statement, in bold above. When only the for statement is found, the for loop ends. The control flow jumps to the statement after the for loop. Program output:
Found 12 at index 4
the unlabelled break statement ends the switch, for, while, do-while statements at the bottom. The label break ends the outermost statement. The following program, breakwithlabeldemo, is similar to the previous program, but uses nested loops to find a value in a two-dimensional array. After the value is found, the label break statement ends the outermost for loop (the label is "Search"):
Class breakwithlabeldemo {public static void main (string [] ARGs) {int [] [] arrayofints = {32, 87, 3,589}, {12,107 6, 2000, 8 }, {622,127, 77,955 }}; int searchfor = 12; int I; Int J = 0; Boolean foundit = false; Search: for (I = 0; 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 ");}}}
Program output:
Found 12 at 1, 0
The break statement ends the label statement, which is not a transfer control flow to the label. Controls are sent to the followed tag (Termination) statement.
Continue statement
The continue statement ignores the current iteration of for, while, do-while. In non-label mode, ignore the inner loop body and calculate the Boolean expression of loop control. The next program, continuedemo, calculates the number of times the letter "P" appears through a string step. If the current character is not P, the continue statement skips the rest of the loopCodeAnd then process the next character. If the current character is P, the program will automatically increase the number of characters.
Class continuedemo {public static void main (string [] ARGs) {string Searchme = "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 ++;} system. out. println ("found" + Numps + "P's in the string. ");}}
Here is the program output:
Found 9 P's in the string.
To better understand the effect, try to remove the continue statement and recompile it. Run the program again. The Count value is incorrect, and the output value is 35 instead of 9.
The label continue statement ignores the current iteration of the outer loop marked by the label. The following program example, continuewithlabeldemo, uses nested loops to search for strings in character passing strings. Two nested loops are required: one iteration string and one iteration string being searched. The following programThe continuewithlabeldemo uses the label form of the continue to ignore the outermost loop.
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 ");}}
Here is the program output:
Found it
Return Statement
The final branch statement is the return statement. The Return Statement exits from the current method, and the control flow is returned to the method call. A return statement has two forms: return value and no return value. To return a value, simply put the value after the return keyword (or put an expression for calculation)
Return ++ count;
The data type of the return value, which must be consistent with the type of the return value declared by the method. When the method is declared as void, the return value is not required to use the following form of return.
Return;