2.4.5、分支语句

这篇Java教程基于JDK1.8。教程中的示例和实践不会使用未来发行版中的优化建议。
分支语句
break语句

break语句有两种形式,带标签的和不带标签的。在switch语句中使用的break语句是不带标签形式的。你可以使用不带标签的break语句来终止for,while和do-while循环,如下所示:

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

该程序在数组中搜索数字12,break语句在找到数字12之后终止了循环。控制流转到了for循环之后的语句。程序输出如下

Found 12 at index 4

不带标签的break语句可以终止switch,for,while和do-while语句的内循环,但只有带标签的break语句可以终止外循环。下面的示例,如上例基本相同,但是使用了for循环在一个二维数组中寻找一个值,当找到这个值之后,将使用带标签的break语句终止循环。

public class BreakWithLabelDemo {

    public static void main(String[] args){
        int[][] arrayOfInts = {
                        {32,87,3,589},
                        {12,1076,2000,8},
                        {622,127}};

        int searchFor = 12;

        int i ,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 index " + i + "," + j);
        }else{
            System.out.println(searchFor + " not in array");
        }
    }
}

程序输出为:

Found 12 at index 1,0

break语句终止了标签语句;不是把控制流专项了标签所在的语句,而是转向了标签语句结尾之后的语句。

continue语句

continue语句跳过for,while和do-while的当次循环。无标签的continue直接跳到循环体的末尾,并计算控制循环的布尔表达式。下面的示例,展示了从一个字符串中计数字符’p’出现的次数。如果当前的字符不是p则跳过当次循环并处理下一个字符;如果是p,增计数器加1。

public 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++){
            if(searchMe.charAt(i) != 'p'){
                continue;
            }
            numPs++;
        }
        System.out.println("Found " + numPs +" p's in the string");
    }
}

程序输出如下:

Found 9 p's in the string

有标签的continue语句将会跳过用给定标签标记的外部循环当中的当前迭代。下面的示例,使用内嵌循环在字符串中查找子串。两层嵌套的循环是必要的:一个遍历子字符串,另一个遍历待查询的字符串。

public 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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值