Java syntax structure and java syntax
I. Ordered Structure(Execute from top to bottom)
The Sequence Structure syntax is relatively simple and can be executed from top to bottom.
Ii. Select Structure(Optional, if... then .....)
1. if statement
The if statement is used to determine whether the result is true or false, and select one of the branches for execution.
If (condition judgment, the result is a Boolean value ){
The execution statement when the condition is true (only one statement can be used. If multiple statements are used, they must be enclosed in braces, we recommend that you add {} to the habit of both one sentence and multiple sentences )}
Else {
Execution statement when the condition is false}
For example, int y =-2;
If (y> 0 ){
System. out. println (y + "is positive ");}
Else {
System. out. println (y + "not positive ");}
2. switch statement
Switch statement, which discusses the results of the expression and determines whether a variable is equal to a value in a series of values. Each value is called a branch.
Switch (expression, result: byte, short, int, char, JDK1.7 and later added support for long and String ){
Case value 1: (the value can only be a literal, not an expression)
Select the statement to be executed when the expression result is 1;
Break;
Case value 2:
Select the statement to be executed when the expression result is 2;
Break;
..........
Case value n:
Select the statement to be executed when the expression result is n;
Break;
[Default:
Statement executed when the expression result is not any of the above values]
}
Iii. Loop Structure(Keep running, when ...)
1. while statement
Syntax 1:
While (condition judgment ){
Statement that is executed cyclically when the condition is true (only one statement can be used. If multiple statements are used, {} braces must be added. {} is recommended for either one or more statements {})}
Execution sequence:
Determine the conditions;
If the result is true, execute the loop statement;
Repeat the preceding two steps. If the result is false, the loop ends.
Syntax 2:
Do {
Statements executed cyclically when conditions are true}
While (condition judgment)
Execution sequence:
Execute loop statements;
Determine the conditions. If the result is true, repeat the two steps;
If the result is false, the loop ends.
Note: Pay attention to the endless loop (ctrl + c Exit the endless loop) and loop boundary.
2. for statement
Syntax 1:
For (variable initialization statement; judgment statement; variable iteration change statement ){
Statement that is executed cyclically when the condition is true (only one statement can be used. If multiple statements are used, {} braces must be added. {} is recommended for either one or more statements {})}
Execution sequence:
Variable initialization;
Determine the conditions;
If the result is true, execute the loop statement;
Variable iteration changes;
Repeat steps 2, 3, and 4 above. If the result is false, the loop ends.
For example: for (int I = 1; I <10; I ++ ){
System. out. println (I );
}
Syntax 2:
For (declarative variable: Set ){
Statements executed cyclically for each data in the Set}
Execution sequence:
Execute loop statements;
Determine the conditions. If the result is true, repeat the two steps;
If the result is false, the loop ends.
For example: int x [] = {457 };
For (int a: x ){
System. out. print (a + "\ t ");
}
System. out. print ();
Supplementary jump statement:
1. break keywords
Break is mainly used in loop or switch statements to jump out of the entire statement block. However, the break only jumps out of the loop at the bottom layer. After it jumps out, it will continue to execute the statements below the loop.
2. continue keywords
Continue applies to any loop control structure. The function is to immediately jump the program to the next iteration of the loop. In the for loop, the continue statement causes the program to jump to the update statement immediately. In the while or do... In the while loop, the program immediately jumps to the judgment statement of the Boolean expression.
Syntax for jump to multi-layer loop:
1) Add the tag at the beginning of the specified loop statement line, syntax → Tag Name: statement;
2) Jump statement: break | The Name Of The continue tag;
3. return keyword
Indicates the end of the method, and subsequent code in the method is not executed.