Java_log2000_关键字break与switch运用辨析

辨析switch及break –不牢靠知识点

break和switch在Java、C/C++语言中运用频繁,相关知识点却总是模糊,这次一下搞通搞透


Java

以一段简单代码为例:

public class LoopAndBreak {
    public static void main(String args[]){
        int i,j;
        for(i=0;i<4;i++){
            System.out.println("outer loop No."+i);
            for(j=5;j>0;j--){
                System.out.println("inner loop No."+j);
                if (j==3) break;
            }
        }
    }

}

程序输出结果为

outer loop No.0
inner loop No.5
inner loop No.4
inner loop No.3
outer loop No.1
inner loop No.5
inner loop No.4
inner loop No.3
outer loop No.2
inner loop No.5
inner loop No.4
inner loop No.3
outer loop No.3
inner loop No.5
inner loop No.4
inner loop No.3

可以看出,外层循环循环了4次,计数器值由0到3,内层循环每次执行3次,到计数器j==3时便跳出内层循环继续下一轮外层循环

再来看一个例子

public class LoopSwitchAndBreak {
    public static void main(String args[]){
        int i,j;
        for(i=0;i<4;i++){
            System.out.println("OUT and i="+i);
            for(j=5;j>0;j--){
                System.out.println("outside of switch and j=="+j);
                switch(j){
                case 5:
                    System.out.println("Inside switch and j=="+j);
                    break;
                case 4:
                    System.out.println("Inside switch and j=="+j);
                    break;
                }
                if (j==3) break;
            }
        }
    }

}

程序输出结果为

OUT and i=0
outside of switch and j==5
Inside switch and j==5
outside of switch and j==4
Inside switch and j==4
outside of switch and j==3
OUT and i=1
outside of switch and j==5
Inside switch and j==5
outside of switch and j==4
Inside switch and j==4
outside of switch and j==3
OUT and i=2
outside of switch and j==5
Inside switch and j==5
outside of switch and j==4
Inside switch and j==4
outside of switch and j==3
OUT and i=3
outside of switch and j==5
Inside switch and j==5
outside of switch and j==4
Inside switch and j==4
outside of switch and j==3

可以看出,switch中的break返回的是内层循环

如果当Java中main方法中switch调用了另一个switch方法呢?

import java.util.Scanner;

public class loopAndbreak {
    public static void foo(int num){
        switch(num){
            case 1:
                System.out.println("foo->case1");
                break;
             case 2:
                System.out.println("foo->case2");
                break;//回到大循环
             default:
                System.out.println("input error");
                //回到大循环
        }
    }

    public static void main(String args[]){
        Scanner sc= new Scanner(System.in);
        int a;
        int b;
        int c;
        do{
            System.out.println("input a ");
            a=sc.nextInt();
            if(!(a>=1 && a<=3)){
                System.out.println("input again");
                a=sc.nextInt();
                }
            System.out.println("a="+a);
            switch(a){
            case 1:
                System.out.println("a->case1");
                System.out.println("b= ");
                b=sc.nextInt();
                if (b>=3){System.out.println("input again");b=sc.nextInt();}
                switch(b){
                case 1:
                    System.out.println("b->case1");
                    System.out.println("input c ");
                    c=sc.nextInt();
                    foo(c);
                    //回到大循环
                    break;
                case 2:
                    System.out.println("b->case2");
                    System.out.println("回到while大循环 ");//回到大循环;
                    break;
                default:
                    System.out.println("still wrong input,going back to main manu");
                    System.out.println("回到while大循环 ");//;
                }
                break;//回到大循环
            case 2:
                System.out.println("a->case2");
                System.out.println("回到大循环");
                break;//回到while大循环
            case 3:
                System.out.println("leave the system");
                System.exit(0);
            default:
                System.out.println("wrong input, going back to main manu");
            }
        }while(true);//大循环
    }
}

这段代码与以下C++代码验证结果相同


C++


#include<iostream>
#include<stdlib.h>
using namespace std;
int foo(int num){
    switch(num){
                    case 1:
                        cout<<"foo->case1"<<endl;
                        break;
                    case 2:
                        cout<<"foo->case2"<<endl;
                        break;//回到大循环
                    default:
                        cout<<"input error,"<<endl;
                        //回到大循环
                }
}
main(){
    int a;
    int b;
    int c;
    do{
        cout<<"input a ";
        cin>>a;
        if(!(a>=1 && a<=3)){
                cout<<"input again";
                cin>>a;
            }
        cout<<"a="<<a<<endl;
        switch(a){
        case 1:
            cout<<"a->case1"<<endl;
            cout<<"b= "<<endl;
            cin>>b;
            if (b>=3){cout<<"input again";cin>>b;}
            switch(b){
            case 1:
                cout<<"b->case1"<<endl;
                cout<<"input c ";
                cin>>c;
                foo(c);
                //回到大循环
                break;
            case 2:
                cout<<"b->case2"<<endl;
                cout<<"回到while大循环 "<<endl;//回到大循环;
                break;
            default:
                cout<<"still wrong input,going back to main manu"<<endl;
                cout<<"回到while大循环 "<<endl;//;
            }
            break;//回到大循环
        case 2:
            cout<<"a->case2"<<endl;
            cout<<"回到大循环"<<endl;
            break;//回到大循环
        case 3:
            cout<<"leave the system";
            exit(0);
        default:
            cout<<"wrong input, going back to main manu"<<endl;
        }
    }while(1);//大循环
}

由此可见,当switch中嵌套switch再调用switch时(3层switch),最内层的break会直接跳出所有层数的switch回到主程序

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值