In the SWITCH statement, if a CASE statement is executed and it is empty, if there is no BREAK, will the DEFAULT statement be executed (other CASEs are not BREAK) 2020928 personal test

Will execute bai, if there is no break after the case; du will continue to execute until there is a break; if not, it will execute to the default statement dao and end the switch statement.

Personally test 2020.9.28

Only execute it once, even the following default conditions are also executed. There will be no so-called infinite loop that I am worried about and has been repeatedly executing the contents of the whole SWITCH from the beginning. Only once to the end

 

 
Java中switch使用格式: switch(表达式) { case 常量表达式1: 语句1; break; .... case 常量表达式2: 语句2; break; default:语句; }

This kind of case is followed by break, as long as the corresponding statement is matched and executed, it will jump out of the switch and execute the following statement. This is easy to understand.

可是如果case后面没有跟上break,程序会如何执行呢?
如下程序,根据数字1-7对应输出相应的英文单词:

https://zhidao.baidu.com/question/34978661.html

Case is just an entrance bai. If there is no break, all zhicases behind du will be executed once from the entrance. For example, in the following program dao:

int x;

switch(x)

{

case 1:printf("a");

case 2:printf("b");

case 3:printf("c");

}

If x=1, because there is no break; statement, the system will execute

printf("a");

printf("b");

printf("c");

Extended information:

The break statement is to interrupt the current loop, or used with label to interrupt the associated statement.

Executing the break statement will exit the current loop or statement and start the script to execute the next statement.

 

Another example

会执行,如果case后面没有break;就会一直执行下去直到有break为止;如果还没有就会执行到default语句,结束switch语句 

https://blog.csdn.net/llerer/article/details/53608687

Guess you like

Origin blog.csdn.net/qq_25814297/article/details/108845521