Java_log2000_关键字break与switch运用辨析

来源:互联网 发布:淘宝上的剃刀龟 编辑:程序博客网 时间:2024/05/22 05:08

辨析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.0inner loop No.5inner loop No.4inner loop No.3outer loop No.1inner loop No.5inner loop No.4inner loop No.3outer loop No.2inner loop No.5inner loop No.4inner loop No.3outer loop No.3inner loop No.5inner loop No.4inner 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=0outside of switch and j==5Inside switch and j==5outside of switch and j==4Inside switch and j==4outside of switch and j==3OUT and i=1outside of switch and j==5Inside switch and j==5outside of switch and j==4Inside switch and j==4outside of switch and j==3OUT and i=2outside of switch and j==5Inside switch and j==5outside of switch and j==4Inside switch and j==4outside of switch and j==3OUT and i=3outside of switch and j==5Inside switch and j==5outside of switch and j==4Inside switch and j==4outside 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回到主程序

0 0