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

How to use the selection statement switch-case of Java

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "how to use switch-case of Java". In daily operation, I believe many people have doubts about how to use switch-case of Java. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to use switch-case of Java!" Next, please follow the editor to study!

1. Introduction of switch-case

A switch-case statement typically consists of a control expression and multiple case.

The types supported by switch-case control expressions are byte, short, char, int, String (Java 7).

Switch-case statements can be converted to and from if-else statements, but in general, switch-case statements are more efficient.

The default in the switch-case statement is executed when the expression cannot find a matching case. But default is not necessary.

Once the case matches, the following code is executed sequentially, regardless of whether the subsequent case statements match or not, until the first break is encountered.

2. Syntax format switch (expression) {case condition 1: statement 1; break; case condition 2: statement 2; break; case condition 3: statement 3; break;. Default: statement;} 3. The specific code uses String str = "C"; switch (str) {case "A": System.out.println ("A"); break; case "B": System.out.println ("B"); break; case "C": System.out.println ("C"); break; default: System.out.println (0);}

Print the results:

4. Common applications

(1) the two values in case perform the same operation.

Public String method (char variable) {switch (grade) {case 'A': System.out.println (excellent); break; case 'B': case 'C': System.out.println (good); break Case 'D': System.out.println ("pass"); break; case 'F': System.out.println ("you need to try harder"); break; default: System.out.println ("unknown level");}}

(2) there is no break statement in case. Starting from the matching case, the values of all subsequent case are output one after another.

/ / pass a value of 3public String method (int variable) {switch (I) {case 9: System.out.println ("9"); case 3: System.out.println ("3"); case 6: System.out.println ("6") Default: System.out.println ("def");}}

Print the results:

three

six

Def

(3) if the current matching case does not have a break statement, then starting from the current case, all subsequent case values will be output one after another. Until the first break statement is encountered, jump out of the judgment.

/ / pass a value of 3public String method (int variable) {switch (I) {case 9: System.out.println ("9"); case 3: System.out.println ("3"); case 6: System.out.println ("6") Break; default: System.out.println ("def");}

Output result

three

six

At this point, the study on "how to use the choice sentence switch-case of Java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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: 221

*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

Development

Wechat

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

12
Report