In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "principle analysis of Java branch structure and loop structure". In daily operation, I believe that many people have doubts about the principle analysis of Java branch structure and loop structure. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "principle analysis of Java branch structure and loop structure". Next, please follow the editor to study!
Process control classification
Sequential statements: branch statements are executed sequentially from top to bottom: execute different statements loop statements according to different conditions: repeat some actions
Single branch conditional judgment statement
Conditional statement
It's just a separate judgment on whether the condition is valid.
The selection structure of if is judged according to the condition and then processed.
Grammar
If (Boolean expression) {/ / statement to be executed if the Boolean expression is true
Note: the condition must be boolean type if with only one statement that can be omitted {}
Double branch conditional judgment statement
If...else... Cases where the conditions are established and those that are not valid need to be dealt with separately.
The if statement can be followed by the else statement, and when the Boolean expression value of the if statement is false, the else statement block is executed.
Grammar
If (conditional) {/ / Code Block 1} else {/ / Code Block 2}
Multi-branch conditional judgment statement
If...else if...else statement
Grammar
If (Boolean expression 1) {/ / if Boolean expression 1 has the value of true execute code} else if (Boolean expression 2) {/ / if the value of Boolean expression 2 is true, execute code} else if (Boolean expression 3) {/ / if the value of Boolean expression 3 is true, execute code} else {/ / if none of the above Boolean expressions execute code for true
Note: multiple conditions use multiple else if, while if can only have one. Else can be omitted, indicating that none of the above conditions are true. Execute else if,else if,else with only one statement can omit {}.
The method of comparing string equality of String type
The string 1.equals ("string 2"); / equal true is not equal false== determines whether the memory address is the same object, not a string value.
Int num2 = input.nextInt (); boolean flag = num2.equals ("+")
Switch statement
The switch statement determines whether a variable is equal to a value in a series of values, each of which is called a branch.
Grammar
Witch (expression) {case value: / / statement break; / / optional case value: / / statement break; / optional / / you can have any number of case statements default: / / optional / / statement}
The difference between switch and multiple if
[1] switch can only judge the condition of equivalence; multiple if can judge any situation, but it is mostly used to judge the range.
[2] the judgment is also equivalent, and the efficiency of switch is higher.
Switch Note:
[1] support tag stacking: empty tags are listed together
[2] multiple conditional judgments can have multiple case tags
[3] default: tags can be omitted
[4] break; ends switch and continues execution without adding
[5] the type of expression should be the same as that of value. It can only be byte,short,int,char,String, enumerated.
Package test;import java.util.Scanner;public class Test {public static void main (String [] args) {Scanner input = new Scanner (System.in); System.out.println ("enter a month:"); int month = input.nextInt (); switch (month) {case 3: case 4: case 5: System.out.println ("Spring"); break; case 6: case 7: case 8: System.out.println ("Summer"); break Case 9: case 10: case 11: System.out.println ("autumn"); break; case 12: case 1: case 2: System.out.println ("winter"); break; default: System.out.println ("incorrect month entered!") ;} System.out.println ("Game Over!");}}
Branch statement nesting
If (condition 1) {if (condition 2) {code block;}}
Note: only when the outer condition 1 is true, can the inner condition 2 be judged.
Loop statement
Loop: an operation that is repeated. Features: cyclic conditional cyclic operation
Classification: non-fixed number of cycles while do-while
Fixed number of cycles for
Unfixed number of cycles
While (conditional) {Loop body; / / Loop Operation}
Features: the condition is judged first, and then the loop operation is performed. To solve the problem of fixed number of cycles: three elements: 1. The initial value of cyclic variable; 2. The termination value of the loop variable (condition) 3. The value of a cyclic variable change (iteration, increment)
Solve the problem of non-fixed number of times: the method of solving the problem: 1. Determine the cycle condition 2. Determine the cycle body
Do-while cycle
Do {cyclic body;} while (cyclic condition)
Note: execute first, then judge the condition.
Fixed number of cycles
For (initial value; conditional termination value; Increment) {Loop operation;}
Note: conditional judgment supports comma expression
Jump statement
Break
[1] used in switch and loops
[2] used in a single-layer loop to end the loop
Continue
[1] the continue statement is used to end the loop
[2] used in loop statements
At this point, the study on the principle analysis of Java branch structure and loop structure 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: 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.