Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the syntax for using switch in java

Shulou Source: shulou.com Published: 2022-05-31 10:39:12 09月23日 Update

This article mainly introduces the relevant knowledge of "what is the syntax for the use of switch in java". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "what is the syntax for the use of switch in java" can help you solve the problem.

Switch (expression)

{

Case constant expression 1: statement 1

....

Case constant expression 2: statement 2

Default: statement

}

Default means to execute it if there is no case that conforms to it. Default is not necessary.

Statements after case can be without curly braces.

The judgment condition of the switch statement can accept int,byte,char,short, but not other types.

Once the case matches, the following program code will be executed sequentially, regardless of whether the following case matches or not, until the break is encountered. Using this feature, several case can execute unified statements.

For example:

Switch (x)

{

Case 1:

Case 2:

Case3: System.out.println ("")

Break

Case4: System.out.println ("hehe")

}

This is the end of the introduction to "what is the syntax for the use of switch in java". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Statements grammars knowledge expressions constants industries different practical no code content practical practical that is parentheses articles methods more conditions cases Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Apple NVidia Shulou Information Redmi