In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you whether there must be a default option in the switch sentence. I hope you will get something after reading this article. Let's discuss it together.
The default option is not required in the switch statement. The switch statement will first find the case value that meets the condition as the entry of the program behind execution. If all the case is not satisfied, find the default entry, and if it cannot find it, exit the entire statement; default is just a backup entry, it doesn't matter whether you have it or not.
Switch is a reserved word in some computer languages, and its function is to make judgments and choices in most cases; it is often used with case, break, and default.
Switch statements can be used to perform different actions based on different conditions.
The switch statement is a conditional selection statement, which first finds the case value that meets the condition as the entry of the program behind execution; if all the case is not satisfied, find the default entry, and exit the entire switch statement if it is not found; so default is just a backup entry, it doesn't matter whether you have it or not.
However, for error checking or logic checking, you should still add the default branch to the switch statement.
For example, the following switch statement is perfectly legal:
Switch (char_code) {case tyt: case'ybeans: printf ("You answered YES!\ n") break case'Names: case'nails: printf ("You answered NO!\ n"); break}
But what happens if an unknown character is passed to the switch statement? At this point, the program will have no output. Therefore, it is best to add a default branch to handle this situation:
.default: printf ("Unknown response:% d\ n", char_code); break.
In addition, the default branch can bring a lot of convenience to logic checking. For example, if you use a switch statement to deal with a fixed number of conditions and think that values other than those conditions are logic errors, you can add a default branch to identify logic errors.
Please look at the following:
Void move_cursor (int direction) {switch (direction) {case UP: cursor_up () break case DOWN: cursor_down () break case LEFT: cursor_left () break case RIGHT: cursor_ right ( ) break default: printf ("Logic error on line number% ldwings!\ n" _ _ LINE__) break}} finished reading this article I believe you have a certain understanding of "whether there must be default options in the switch sentence". If you want to know more about it, please follow the industry information channel. Thank you for your reading!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.