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

How to execute the switch statement in java

WBOY
Release: 2023-04-18 21:38:20
forward
1659 people have browsed it

1. Syntax

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

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
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template