break语句

break语句有两种形式:标记和未标记。您在先前讨论中看到了未标记的switch语句。您还可以使用未标记的break语句终止forwhiledo-while循环,如下面的 BreakDemo程序:

  1. class BreakDemo {
  2. public static void main(String[] args) {
  3. int[] arrayOfInts =
  4. { 32, 87, 3, 589,
  5. 12, 1076, 2000,
  6. 8, 622, 127 };
  7. int searchfor = 12;
  8. int i;
  9. boolean foundIt = false;
  10. for (i = 0; i < arrayOfInts.length; i++) {
  11. if (arrayOfInts[i] == searchfor) {
  12. foundIt = true;
  13. break;
  14. }
  15. }
  16. if (foundIt) {
  17. System.out.println("Found " + searchfor + " at index " + i);
  18. } else {
  19. System.out.println(searchfor + " not in the array");
  20. }
  21. }
  22. }

该程序在数组中搜索数字12。发现该值时,break语句终止for循环。然后,控制流在for循环后转移到该语句。该程序的输出为:

  1. Found 12 at index 4

未标记的break语句终止最内层switchforwhile,或do-while语句,但标记的break终止一个外部语句。以下程序 BreakWithLabelDemo类似于先前的程序,但是在二维数组中使用嵌套for循环搜索值。找到该值后,带有标记的标记将break终止外for循环(标记为“search”):

  1. class BreakWithLabelDemo {
  2. public static void main(String[] args) {
  3. int[][] arrayOfInts = {
  4. { 32, 87, 3, 589 },
  5. { 12, 1076, 2000, 8 },
  6. { 622, 127, 77, 955 }
  7. };
  8. int searchfor = 12;
  9. int i;
  10. int j = 0;
  11. boolean foundIt = false;
  12. search:
  13. for (i = 0; i < arrayOfInts.length; i++) {
  14. for (j = 0; j < arrayOfInts[i].length;
  15. j++) {
  16. if (arrayOfInts[i][j] == searchfor) {
  17. foundIt = true;
  18. break search;
  19. }
  20. }
  21. }
  22. if (foundIt) {
  23. System.out.println("Found " + searchfor + " at " + i + ", " + j);
  24. } else {
  25. System.out.println(searchfor + " not in the array");
  26. }
  27. }
  28. }

这是程序的输出。

  1. Found 12 at 1, 0

break语句终止带标签的语句;它不会将控制流转移到标签上。在标记(终止)的语句之后,控制流立即转移到该语句。

continue语句

continue语句跳过forwhiledo-while循环的当前迭代。未标记的形式跳到最里面的循环主体的末尾,并评估控制该循环的boolean表达式。以下程序 ContinueDemo逐步执行字符串,计算字母“p”的出现。如果当前字符不是p,则该continue语句将跳过循环的其余部分,并继续执行下一个字符。如果它 “p”,则程序将增加字母数量。

  1. class ContinueDemo {
  2. public static void main(String[] args) {
  3. String searchMe = "peter piper picked a " + "peck of pickled peppers";
  4. int max = searchMe.length();
  5. int numPs = 0;
  6. for (int i = 0; i < max; i++) {
  7. // interested only in p's
  8. if (searchMe.charAt(i) != 'p')
  9. continue;
  10. // process p's
  11. numPs++;
  12. }
  13. System.out.println("Found " + numPs + " p's in the string.");
  14. }
  15. }

这是该程序的输出:

  1. Found 9 p's in the string.

若要更清楚地看到此效果,请尝试删除该continue语句并重新编译。当您再次运行该程序时,计数将是错误的,它找到的是35个p而不是9个。

带标签的continue语句跳过用给定标签标记的外部循环的当前迭代。下面的示例程序ContinueWithLabelDemo使用嵌套循环,在另一个字符串中搜索子字符串。需要两个嵌套循环:一个循环访问子字符串,另一个循环访问要搜索的字符串。以下程序 ContinueWithLabelDemo使用标记形式的Continue跳过外循环中的迭代。

  1. class ContinueWithLabelDemo {
  2. public static void main(String[] args) {
  3. String searchMe = "Look for a substring in me";
  4. String substring = "sub";
  5. boolean foundIt = false;
  6. int max = searchMe.length() -
  7. substring.length();
  8. test:
  9. for (int i = 0; i <= max; i++) {
  10. int n = substring.length();
  11. int j = i;
  12. int k = 0;
  13. while (n-- != 0) {
  14. if (searchMe.charAt(j++) != substring.charAt(k++)) {
  15. continue test;
  16. }
  17. }
  18. foundIt = true;
  19. break test;
  20. }
  21. System.out.println(foundIt ? "Found it" : "Didn't find it");
  22. }
  23. }

这是该程序的输出。

  1. Found it

return语句

分支语句的最后一个是return语句。return语句从当前方法退出,控制流返回到调用该方法的位置。return语句有两种形式:一种返回值,另一种不返回值。要返回值,只需将值(或计算值的表达式)放在关键字return之后。

  1. return ++count;

返回值的数据类型必须与方法声明的返回值的类型匹配。声明方法void时,使用不返回值形式的return

  1. return;

类和对象”课程将涵盖您需要了解的有关编写方法的所有内容。