简体   繁体   English

在switch语句中以默认情况继续

[英]Having a continue in a default case in a switch statement

If I had this: 如果我有这个:

do {
    int x = scannerScan.nextInt();

    switch(x)
    {
        case 1:
            System.out.println("Stuff");
            break;
        case 2:
            System.out.println("Pink cows are fluffy and can fly.");
        default:
            continue;
    }
}
while(true);

What would happen if the default case were to be reached? 如果要达到默认情况会发生什么? I tried to find stuff on the internet and Stackoverflow, but could not find anything about a continue in the default case that had to do with the Java language. 我试图在互联网和Stackoverflow上找到东西,但在与Java语言有关的默认情况下找不到关于继续的任何内容。

continue statement in the loop 循环中的continue语句

The continue statement skips the current iteration of a for , while , or do-while loop. continue语句跳过forwhiledo-while循环的当前迭代。 The unlabeled form skips to the end of the innermost loop's body and evaluates the boolean expression that controls the loop. 未标记的表单跳到最内层循环体的末尾,并计算控制循环的布尔表达式。 [...] [...]

In your code the loop while(true); 在你的代码中循环while(true); will continue. 将继续。 The statement makes no effect on the switch code block. 该语句对switch代码块没有影响。

continue statements in switch statements are not special. switch语句中的continue语句并不特殊。 It would jump to the loop condition (the end of the loop body), just as it would if it was inside the loop but outside the switch . 它将跳转到循环条件(循环体的末端),就像它在循环内但在switch外部一样。

In this particular code snippet, it effectively does nothing. 在这个特定的代码片段中,它实际上什么都不做。

Compare break statement : 比较break语句

A break statement attempts to transfer control to the innermost enclosing switch , while , do , or for statement … break语句尝试将控制转移到最里面的封闭switchwhiledofor语句......

With continue statement : 随着continue的语句

A continue statement attempts to transfer control to the innermost enclosing while , do , or for statement … continue语句尝试将控制转移到最内层的whiledofor语句...

Thus, continue refers to the do...while loop, and: 因此, continue指的是do...while循环,并且:

… then immediately ends the current iteration and begins a new one. ...然后立即结束当前迭代并开始新的迭代。

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

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