Home >Java >javaTutorial >What is the syntax for using switch in java

What is the syntax for using switch in java

PHPz
PHPzforward
2023-05-16 12:01:111744browse

switch(expression)
{
case constant expression 1: statement 1;
....
case constant expression 2: statement 2;
default: statement;
}
Default is to execute it if there is no matching case. Default is not necessary.
The statement after the case does not need curly brackets.
The judgment condition of the switch statement can accept int, byte, Char, short, cannot accept other types.
Once the case matches, the following program code will be executed sequentially, regardless of whether the subsequent cases match, until break is encountered. This feature can be used to allow several cases to execute the same statement.

For example:

switch(x)

{

case 1:

case 2:

case3 : System.out.println("haha");

break;

case4: System.out.println("hehe");

}

The above is the detailed content of What is the syntax for using switch 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