Home > Java > javaTutorial > body text

What is the syntax for using switch in java

PHPz
Release: 2023-05-16 12:01:11
forward
1622 people have browsed it

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!