文档

Java™教程
隐藏目录
分支语句
路径:学习Java语言
课程:语言基础
章节:控制流语句

分支语句

break语句

break语句有两种形式:带标签和不带标签。你在之前对switch语句的讨论中看到了不带标签的形式。你也可以使用不带标签的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("找到了 " + searchfor + " 在索引 " + i);
        } else {
            System.out.println(searchfor + " 不在数组中");
        }
    }
}

这个程序在数组中搜索数字12。加粗显示的break语句在找到该值时终止for循环。控制流然后转移到for循环后的语句。该程序的输出是:

找到了 12 在索引 4

不带标签的break语句终止最内层的switch、for、while或do-while语句,但带标签的break语句终止外部语句。下面的程序BreakWithLabelDemo与之前的程序类似,但使用嵌套的for循环在二维数组中搜索一个值。当找到该值时,带标签的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("找到了 " + searchfor + " 在 " + i + ", " + j);
        } else {
            System.out.println(searchfor + " 不在数组中");
        }
    }
}

这是程序的输出。

在 1, 0 处找到 12

break 语句终止带标签的语句; 它不会将控制流转移到标签处。控制流转移到标记(已终止)语句后面的语句。

continue 语句

continue 语句跳过当前的 forwhiledo-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++) {
            // 只关注 p
            if (searchMe.charAt(i) != 'p')
                continue;

            // 处理 p
            numPs++;
        }
        System.out.println("在字符串中找到了 " + numPs + " 个 p。");
    }
}

这个程序的输出如下:

在字符串中找到了 9 个 p。

为了更清楚地看到这个效果,尝试删除 continue 语句并重新编译。当你再次运行程序时,计数将是错误的,显示找到了 35 个 p,而不是 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 ? "找到了" : "没有找到");
    }
}

这是程序的输出结果。

找到了

return语句

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

return ++count;

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

return;

类和对象教程将涵盖关于编写方法的所有知识。


上一页: for语句
下一页: 控制流程语句概述