【JavaSE】break、continue、return语句
声明:以下文章来源ORACLE的JDK8教程,官网直达链接https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
点击下列4个标题可直达相应的列子
code example 1:初识break:无标签break
code example 2:再识break:带标签break
code example 3:初识continue:不带标签continue
code example 4:再识continue:带标签continue
break语句
The break statement has two forms: labeled and unlabeled. You saw the unlabeled form in the previous discussion of the switch statement. You can also use an unlabeled break to terminate a for, while, or do-while loop, as shown in the following BreakDemo program:
break有两种形式:带标签的和不带标签的。switch语句里的break就是不带标签的形式,你也可以在for、while、do-while里使用不带标签的break来终止循环。
code example 1:初识break:无标签break
1 public class BreakDemo { 2 3 public static void main(String[] args) { 4 5 int[] arrayOfInts = 6 { 32, 87, 3, 589, 12, 1076, 2000, 8, 622, 127 }; 7 int searchfor = 12; 8 9 int i; 10 boolean foundIt = false; 11 12 for (i = 0; i < arrayOfInts.length; i++) { 13 if (arrayOfInts[i] == searchfor) { 14 foundIt = true; 15 break; 16 } 17 } 18 19 if (foundIt) { 20 System.out.println("Found " + searchfor + " at index " + i); 21 } else { 22 System.out.println(searchfor + " not in the array"); 23 } 24 } 25 26 }
该程序在数组中搜索12,break语句在这里的作用是:找到该值时终止for循环
This program searches for the number 12 in an array. The break statement, shown in boldface, terminates the for loop when that value is found. Control flow then transfers to the statement after the for loop. This program's output is:
Found 12 at index 4
code example 2:再识break:带标签break
An unlabeled break statement terminates the innermost switch, for, while, or do-while statement, but a labeled break terminates an outer statement. The following program, BreakWithLabelDemo, is similar to the previous program, but uses nested for loops to search for a value in a two-dimensional array. When the value is found, a labeled break terminates the outer for loop (labeled "search"):
无标签的break语句终止for、while、do-while最里面的开关,但是带标签的break会终止某个外部语句。
1 class BreakWithLabelDemo { 2 public static void main(String[] args) { 3 4 int[][] arrayOfInts = { 5 { 32, 87, 3, 589 }, 6 { 12, 1076, 2000, 8 }, 7 { 622, 127, 77, 955 } 8 }; 9 int searchfor = 12; 10 11 int i; 12 int j = 0; 13 boolean foundIt = false; 14 15 search: 16 for (i = 0; i < arrayOfInts.length; i++) { 17 for (j = 0; j < arrayOfInts[i].length; 18 j++) { 19 if (arrayOfInts[i][j] == searchfor) { 20 foundIt = true; 21 break search; 22 } 23 } 24 } 25 26 if (foundIt) { 27 System.out.println("Found " + searchfor + " at " + i + ", " + j); 28 } else { 29 System.out.println(searchfor + " not in the array"); 30 } 31 } 32 }
This is the output of the program.
Found 12 at 1, 0
The break statement terminates the labeled statement; it does not transfer the flow of control to the label. Control flow is transferred to the statement immediately following the labeled (terminated) statement.
break语句终止带标签的语句;它不是将控制流转移到标签上。而是控制流随着break search语句执行后才会根据break后的search标签立刻找到外部search标签进行的转移。
continue语句
The continue statement skips the current iteration of a for, while , or do-while loop. The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop. The following program, ContinueDemo , steps through a String, counting the occurences of the letter "p". 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 the letter count.
Continue语句跳过for,while或do-while循环的当前迭代。
code example 3:初识continue:不带标签continue
1 class ContinueDemo { 2 public static void main(String[] args) { 3 4 String searchMe = "peter piper picked a " + "peck of pickled peppers"; 5 int max = searchMe.length(); 6 int numPs = 0; 7 8 for (int i = 0; i < max; i++) { 9 // interested only in p's 10 if (searchMe.charAt(i) != 'p') 11 continue; 12 13 // process p's 14 numPs++; 15 } 16 System.out.println("Found " + numPs + " p's in the string."); 17 } 18 }
Here is the output of this program:
Found 9 p's in the string.
To see this effect more clearly, try removing the continue statement and recompiling. When you run the program again, the count will be wrong, saying that it found 35 p's instead of 9.
为了更清楚地看到continue语句的作用,试试去掉continue再运行,运行结果会变为Found 35 p's in the string.
code example 4:再识continue:带标签continue
A labeled continue 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. The following program, ContinueWithLabelDemo, uses the labeled form of continue to skip an iteration in the outer loop.
带标签的continue语句会跳过当前迭代并寻找、执行标有给定标签的外部循环。
1 public class ContinueWithLabelDemo { 2 3 public static void main(String[] args) { 4 5 String searchMe = "Look for a substring in me"; 6 String substring = "sub"; 7 boolean foundIt = false; 8 9 int max = searchMe.length() - substring.length();//23 10 11 test: 12 for (int i = 0; i <= max; i++) { 13 int n = substring.length();//3 14 int j = i;//0 15 int k = 0; 16 while (n-- != 0) {//3 begin 17 if (searchMe.charAt(j++) != substring.charAt(k++)) { 18 continue test; 19 } 20 } 21 foundIt = true; 22 break test; 23 } 24 System.out.println(foundIt ? "Found it" : "Didn't find it"); 25 } 26 }
Here is the output from this program.
Found it
return语句
The last of the branching statements is the return statement. The return statement exits from the current method, and control flow returns to where the method was invoked. The return 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 the return keyword.
return语句有两种形式:一种返回值,另一种不返回值。要返回值,只需把值(或计算值的表达式)放在return关键字之后。
return ++count;
The data type of the returned value must match the type of the method's declared return value. When a method is declared void, use the form of return that doesn't return a value.
返回值的数据类型必须与方法声明的返回值的类型匹配。当方法声明为void时,请使用不返回值的return形式。
return;

浙公网安备 33010602011771号