Home >Java >javaTutorial >How to execute the switch statement in java

How to execute the switch statement in java

WBOY
WBOYforward
2023-04-18 21:38:201676browse

1. Syntax

switch(常量)  {
case 表达式1:语句体1;
break;
case 表达式2:语句体2;
break;
.....
default :  语句体n;
break;
}

2. Execution process

(1) case checks whether it matches according to its own expression constant. If there is a match, execute the statement body, otherwise execute the default statement.

(2) case execution is similar to parallel operations, not sequential operations. So the value of each expression cannot be the same. Which case matches the constant will execute its own sentence and will not look for other case sentences.

3. Example

int  i  = 10;
switch (i ) {
case 10:
System.out .println(" A" );
//break;
case 5:
System.out .println(" B" );
//break;
case 7:
System.out .println(" C" );
//break;
default :
System.out .println(" error");
break;
}//打印结果:A B C error

The above is the detailed content of How to execute the switch statement in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete