zoukankan      html  css  js  c++  java
  • java break语句的三种用法

    1、用于switch语句当中,用于终止语句

    2、用于跳出循环,此为不带标签的break语句,相当与goto的作用

    e.g

     1         while(i<j&&h<k){
     2             if(h<k)
     3             {
     4                 ....
     5             }
     6         }
     7         
     8         while(i<j){
     9             if(h>k)  break;
    10         }

    在第一种写法中,对条件h<k进行了两次检测,而第二种写法,通过使用break跳出循环,减少了对同一条件的判别次数,避免了重复检测。

    注意,在一系列循环嵌套中,break仅仅终止的是最里层的循环

    试验代码如下:

     1 for(int i=0;i<=3;i++){
     2             System.out.print("loop"+i+" ");
     3             for(int j=0;j<10;j++){
     4             if(j==3){
     5                 break;
     6             }
     7             System.out.print("j="+j+" ");
     8             }
     9             System.out.println();
    10             
    11         }

    输出:

    loop0 j=0 j=1 j=2
    loop1 j=0 j=1 j=2
    loop2 j=0 j=1 j=2
    loop3 j=0 j=1 j=2

    3、带标签的break语句

    常常用于跳出多层嵌套

    注意,在带标签的break语句中,标签必须放在希望跳出的最外层之前,并紧跟一个冒号

    e.g

     1 public class Test {
     2     public static void main(String args[]){
     3         
     4         read_data:
     5             while(1==1){
     6                 if(1==1){
     7                     System.out.println("1st");
     8                         
     9                 }
    10                 if(1==1){
    11                     System.out.println("2nd");
    12                     break read_data;
    13                     
    14                 }
    15                 if(1==1){
    16                     System.out.println("3rd");
    17                 }
    18             }
    19         
    20         System.out.println("out of loop");
    21         
    22         
    23     }
    24 }

    输出:

    1st
    2nd
    out of loop

    e.g:

     1         first:{
     2         System.out.println("first");
     3         second:{
     4             System.out.println("second");
     5             if(1==1){
     6                 break first;
     7             }
     8             third:{
     9                 System.out.println("third");
    10             }
    11         }
    12         System.out.println("will not be excuted");
    13     }
    14     System.out.println("out of loop");

    输出:

    first
    second
    out of loop


    对于带标签的break语句而言,只能跳转到包含break语句的那个块的标签上

    下列代码是非法的:

    1         first:if(1==1){
    2             System.out.print("................");
    3         }
    4         second:if(1==1){
    5             System.out.print("................");
    6             break first;
    7         }    

    补充 continue语句:

    continue语句将控制转移到最内层循环的首部

    while和 do while语句中continue语句直接转移到条件表达式,在for循环中,循环的检验条件被求值。

    e.g:

     1 public static void main(String args[]){
     2         
     3         int count;
     4         int n;
     5         int sum=0;
     6         Scanner i=new Scanner(System.in);
     7         for(count=1;count<=3;count++){
     8             System.out.println("enter a number,-1 to quit:");
     9             n=i.nextInt();
    10             if(n<0) 
    11                 {            
    12                 System.out.println("sum="+sum);
    13                 continue;
    14                 }
    15             sum+=n;
    16             System.out.println("sum="+sum);
    17         }
    18 
    19     }

    输出:

    enter a number,-1 to quit:
    1
    sum=1
    enter a number,-1 to quit:
    -1
    sum=1
    enter a number,-1 to quit:
    1
    sum=2
    当输入为负时,continue语句直接跳到count++语句

    continue语句也可以使用标签:

    e.g

     1 public static void main(String args[]) {
     2 
     3         outer: for (int i=0; i<10; i++) {
     4 
     5         for(int j=0; j<10; j++) {
     6 
     7         if(j > i) {
     8 
     9         System.out.println();
    10 
    11         continue outer; }
    12 
    13         System.out.print(" " + (i * j)); }}
    14 
    15         System.out.println();
    16 
    17         } 

    输出:

     0
     0 1
     0 2 4
     0 3 6 9
     0 4 8 12 16
     0 5 10 15 20 25
     0 6 12 18 24 30 36
     0 7 14 21 28 35 42 49
     0 8 16 24 32 40 48 56 64
     0 9 18 27 36 45 54 63 72 81

    return语句:

    return语句将程序控制返回到调用它的方法

    e.g:

     1 public static void main(String args[]) {
     2         
     3         for(int i=0;i<10;i++){
     4         System.out.println("i="+i+" ");
     5             for(int j=0;j<10;j++)
     6             {
     7                 if(1==1){
     8                 return;
     9                 }
    10                 System.out.println("j="+j+" ");
    11             }
    12         }
    13         }

    输出:

    i=0

    return 用来是正在执行的分支程序返回到它的调用方法上,在此,main函数的调用方法是java运行系统,所以执行到return;处,程序控制将被传递到它的调用者,所以后面的代码将都不被执行。

  • 相关阅读:
    redis持久化(RDB、AOF、混合持久化)
    springboot将JSON引擎替换为fastJSON
    springboot项目中的拦截器
    Flutter——扫描条形码以及二维码
    Flutter——消息框(Fluttertoast)
    Flutter——AlertDialog组件、SimpleDialog组件、showModalBottomSheet组件(弹窗组件)
    Fragment懒加载
    permissionsdispatcher的用法(用起来并没有多好用)
    Android串口(SerialPort) java.lang.SecurityException
    Integer值的比较存在的坑
  • 原文地址:https://www.cnblogs.com/wpzy2311/p/4222810.html
Copyright © 2011-2022 走看看