Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the format of the switch statement

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail what the format of the switch sentence is. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The format of the switch statement is [switch (expression) {case value 1: statement body 1: Breaking case value 2: statement body 2: statement body 2;]; where breaktable indicates the meaning of break, and when all values do not match, the statement body default is executed.

The format of the switch statement is:

1. Switch statement format:

Switch (expression) {case value 1: statement aspect 1; break; case value 2: statement aspect 2; break;. Default: sentence body nasty 1; break;}

2. Format explanation:

Expression: byte,short,int,char

The value after case: is what is used to match the value of the expression

Break: the meaning of table break

Default: when all the values do not match, execute the statement body default

3. Execution process:

A: first evaluate the value of the expression

B: compare the calculated value with the value after case in turn. Once there is a match, the corresponding statement is executed and ends when break is encountered.

C: if all the case do not match, execute the body of the statement

4. Example code:

Scanner sc= new Scanner (System.in); System.out.println ("Please enter the number 1-7"); int I = sc.nextInt () Switch (I) {case 1: System.out.println (Monday); break Case 2: System.out.println (Tuesday); break; case 3: System.out.println (Wednesday); break Case 4: System.out.println (Thursday); break; case 5: System.out.println (Friday); break Case 6: System.out.println (Saturday); break; case 7: System.out.println (Sunday 1); break Default: System.out.println ("Please enter the correct number:"); break; on how the format of the switch sentence is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report