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

The use principle and example Analysis of Javaswitch

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "the principle of the use of Javaswitch and example analysis", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "the principle of the use of Javaswitch and example analysis"!

There are two commonly used conditional judgment methods in java, one is if {} else {}, and the other is switch (). Generally speaking, because switch can directly meet the conditions one-to-one, the situation that does not meet the conditions will not be implemented, so the efficiency of switch will be higher than that of if {} else {}, and the two methods can be interchangeable without threshold.

Use of switch:

The types that can be used for switch judgment are: byte, short, int, char (JDK1.6), and enumerated types, but after JDK1.7, the case statement for judging the type of String is written less than break, and the compiler will not report an error, but it will execute all the statements under case conditions without judgment until the default statement executes the code block under default if there is no qualified case. Default is not necessary or may not be written.

Switch (mark) {case 0: System.out.println (mark); break; case 10: System.out.println (mark); break; case 20: System.out.println (mark); break;}

Here, the mark value is the judgment condition, and case corresponds to the specific value. If mark=0 or mark=10 or mark=20, the corresponding condition is true, the program in case will be executed.

Here is a misunderstanding that is easy to make mistakes. Take a look at the following code:

Switch (mark) {case 0: System.out.println (mark); mark = 10; break; case 10: System.out.println (mark); mark = 20; break; case 20: System.out.println (mark); mark = 30; break;}

Compared to the code above, the mark is re-assigned in each case so that the re-assigned value corresponds to the next case.

Wrong thinking: if the initial value of mark is 0, the switch program will be executed three times, in the order of 0, 10, 10, 20.

Because we reassign the mark in each step of the case, it is easy for the unfamiliar to think that the switch will be executed from top to bottom, and that all steps that meet the conditions will be performed. In fact, this switch will only be executed once, because each case is followed by a break, and the function of this break is to jump out of the current loop, that is, out of the current switch, so this switch will only execute the case corresponding to the initial value of the mark, and the following steps will not be carried out. If you want to execute all three steps in turn, you need to add a while loop to the switch, and repeat the switch under certain conditions: add while as follows

While (mark

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

Development

Wechat

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

12
Report