简体   繁体   English

为什么 switch 会执行(int 类型)不匹配的情况?

[英]Why does switch execute (int type)cases that aren't matched?

I am a little puzzled why all cases are executed here, even the ones that aren't matched if I remove the break statement here:我有点疑惑为什么所有的情况都在这里执行,即使是那些如果我在这里删除 break 语句不匹配的情况:

int i = 0;
    switch ( i ) {
        case 0: System.out.print (i) ;
        case 1: System.out.print (i) ;
        case 2: System.out.print (i) ;
        case 3: System.out.print (i) ;
        default : System.out.print (i) ;
    }

This code prints out 5 times the value i.此代码打印出 i 值的 5 倍。 If I were to add a break after case 0, it just prints the value out once.如果我要在 case 0 之后添加一个中断,它只会将值打印一次。

Reading the documentation and the function description in some books, i had expected it to only print the matching case.阅读一些书籍中的文档和 function 描述,我原以为它只会打印匹配的案例。

Is this because it's somehow enumerated?这是因为它以某种方式被枚举了吗? I'm sorry I couldn't find a much better explanation and I've searched extensively, so I figured it has been asked before and I am no good at searching, or it's too basic.很抱歉我找不到更好的解释,而且我已经进行了广泛的搜索,所以我认为以前有人问过它,我不擅长搜索,或者它太基础了。

You must enter a break command after each read, in order to exit the switch !您必须在每次读取后输入break命令,才能退出switch

int i = 0;
    switch ( i ) {
        case 0: System.out.print (i) ;
        break ;
        case 1: System.out.print (i) ;
        break ;
        case 2: System.out.print (i) ;
        break ;
        case 3: System.out.print (i) ;
        break ;
        default : System.out.print (i) ;
       break ;
    }

The switch enter to where the condition is true, After that, it executes all the lines of code that comes after. switch进入条件为真的地方,之后,它执行后面的所有代码行。 Witout break it will not exit and And run the following code lines after.没有break它不会退出并在之后运行以下代码行。

In java switch statements, once it matches a case , all the case clauses after the matching clause are executed sequentially.在 java switch语句中,一旦匹配到一个case ,匹配子句之后的所有case子句都会依次执行。 This fall-through is the expected behavior.这种失败是预期的行为。 If you need to stop this, then break at each case so that, once a case is matched, it will execute only that case and then breaks from the switch block.如果您需要停止此操作,则在每个casebreak ,以便在匹配case后,它将仅执行该case ,然后从switch块中中断。

If your input does not match any of the case blocks, the default case is executed.如果您的输入与任何case块不匹配,则执行default case。

This is from the official javadoc这是来自官方的javadoc

The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered. break 语句是必要的,因为没有它们,switch 块中的语句会失败:匹配 case label 之后的所有语句都按顺序执行,无论后续 case 标签的表达式如何,直到遇到 break 语句。

Try the code with different i values and you'll see it for yourself how the switch behaves.尝试具有不同i值的代码,您将亲眼看到switch的行为方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM