java 分支语句(翻译自Java Tutorials)

break语句

break语句有两种形式:标签和非标签。在前面的switch语句,看到的break语句就是非标签形式。可以使用非标签break,结束for,while,do-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语句结束最里面的switch,for,while,do-while语句。而标签break结束最外面的语句。接下来的程序,BreakWithLabelDemo,类似前面的程序,但使用嵌套循环在二维数组里寻找一个值。但值找到后,标签break语句结束最外面的for循环(标签为"search"):

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语句忽略for,while,do-while的当前迭代。非标签模式,忽略最里面的循环体,然后计算循环控制的boolean表达式。接下来的程序,ContinueDemo,通过一个字符串的步骤,计算字母“p”出现的次数。如果当前字符不是p,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.

为了更清晰看效果,尝试去掉continue语句,重新编译。再跑程序,count将是错误的,输出是35,而不是9.

标签continue语句忽略标签标记的外层循环的当前迭代。下面的程序例子,ContinueWithLabelDemo,使用嵌套循环在字符传的字串中搜索字串。需要两个嵌套循环:一个迭代字串,一个迭代正在被搜索的字串。下面的程序ContinueWithLabelDemo,使用continue的标签形式,忽略最外层的循环。

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;

return的值的数据类型,必须和方法声明的返回值的类型符合。当方法声明为void,使用下面形式的return不需要返回值。

return;
原文地址:https://www.cnblogs.com/ggjucheng/p/2820843.html