Branching 语句

中断声明

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

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;
        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");
        }
    }
}

该程序在数组中搜索数字 12.找到该值时,以粗体显示的break语句终止for循环。然后,控制流在for循环之后转移到该语句。该程序的输出为:

Found 12 at index 4

未标记的break语句终止最里面的switchforwhiledo-while语句,但是标记的break终止外部的语句。下面的程序BreakWithLabelDemo与前面的程序类似,但是使用嵌套的for循环在二维数组中搜索值。找到该值时,标记为break会终止外部for循环(标记为“搜索”):

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;
        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");
        }
    }
}

这是程序的输出。

Found 12 at 1, 0

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

continue 声明

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

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.");
    }
}

这是该程序的输出:

Found 9 p's in the string.

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

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

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声明

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

return ++count;

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

return;

类和对象类将涵盖您需要有关编写方法的所有知识。