In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail the usage examples of the selected structure in Java. 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 basic structure of Java programming-selection structure
Text keywords: basic structure, selection structure, if, else, switch, case
I. if-else structure
When using if-else to select structures, one of the most basic principles is that there must be a judgment condition, and if there is an else, there must be a corresponding if.
Code format: if (judgment condition) {} else {}
The judgment condition part can directly use the Boolean value: true/false
If you use an expression as a judgment condition, you must ensure that the final evaluation result is of Boolean type.
For the switch-case structure, the overall structure is different from if-else, although it is also to judge, but switch-case is to specify a variable or expression as the value to be compared, list the values that match it through case, and write the code that needs to be executed in this case.
Code format: switch (variable or expression) {case match: code snippet; default: code snippet;}
The type of a variable or expression result can only be: integer type (byte, short, int), string String, character char, enumeration enum
Case itself has the feature of penetration, and will enter the execution from a matching case until it encounters break;.
The default keyword may not appear, and if it appears only in the last location, it will be executed if none of the case matches (if the nearest case to the default does not use break, it may be executed)
1. Single if structure
The single if structure is the simplest choice structure, which means: execute part of the code when the condition is met, do nothing when the condition is not met, and continue to execute the code after the selection structure. It is important to note that if holds a condition in parentheses and can only control one statement by default. If you need to control multiple statements, you need to use curly braces.
Public static void main (Stirng [] args) {/ / if you go home every day to report your exam results and finish your homework / / if you do well in the exam (above 90 points), you will get a compliment / / if you don't do well in the exam, nothing will happen / / and, no matter how well you do in the exam, you have to finish your homework. It is so sad that int score = 92 If (score > = 90) {/ / to avoid unnecessary trouble, we add curly braces even for a line of code, which works! System.out.println ("good exam!") ;} System.out.println ("start homework");} 2. If-else structure
If-else structure is the most basic selection structure. After defining a judgment condition, what to do when the condition is established and what to do when the condition is not established.
Public static void main (Stirng [] args) {/ / if there are the following scenarios: students in different grades enter different activity classrooms / / students in grades 3 and below enter activity room 1 / / other grades enter activity classroom 2 int grade = 2; if (grade
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.