Alibaba Java Development Manual Questions about switch
代言
代言 2017-06-12 09:27:30
0
1
1016

In a switch block, each case must be terminated by break/return, etc., or a comment indicating which case the program will continue to execute; Within a switch block, a default statement must be included And
is placed at the end, even if it has no code.
why is that?

代言
代言

reply all(1)
伊谢尔伦

The default in the switch case is very necessary, even if it is just a console output. For example, you have three cases, '1', '2', '3'. But in some cases you accidentally pass in 4. At this time, if there is no default, the program will crash. Sometimes you can see the default It is a type of exception handling. Of course, default is also a good choice for lazy people to deal with problems. It is still the same as 123 before. When there is only 1, it outputs 'hello world', and when it is 1, it outputs 'nice to meet you'. If you don't use default, you may have to write several cases to correspond. If default is used, just one case is enough. Except for 1, all others are default.
The problem of return/break is still the three cases above, 1, 2, and 3. If the logic of 1 is executed without return/break, the logic of 2 will also be executed until return/break is encountered. Of course this situation is legal. But sometimes we only want to execute the logic in case 1, so we must add return/break after the logic of case 1, otherwise the extra execution of the logic of case 2 will lead to logic errors in other places.

That’s probably what it means, there’s not much content.

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!