Process control statement
1, Conditional statement
1. if conditional statement
-
Simple conditional statement
-
Syntax:
if(Boolean expression){ Statement sequence; //The statement that will be executed if the Boolean expression is true }
-
graphic
-
-
Complex jump statements (to shorten the length)
-
If... else if... else if syntax
if(Boolean expression 1){ //If the value of Boolean expression 1 is true, execute the code }else if(Boolean expression 2){ //If the value of Boolean expression 2 is true, execute the code }else if(Boolean expression 3){ //If the value of Boolean expression 3 is true, execute the code }else { //If none of the above Boolean expressions is true, execute the code }
-
graphic
-
Nested if... else syntax
if(Boolean expression 1){ If the value of Boolean expression 1 is true Execute code if(Boolean expression 2){ If the value of Boolean expression 2 is true Execute code } }
-
-
2. switch branch statement
-
grammar
switch(expression){ case value : //sentence break; //Optional case value : //sentence break; //Optional //You can have any number of case statements default : //Optional //sentence }
-
rule
- The variable type in the switch statement can be byte, short, int or char. Starting from Java SE 7, switch supports String type, and the case label must be a String constant or literal.
- A switch statement can have multiple case statements. Each case is followed by a value to compare and a colon.
- The data type of the value in the case statement must be the same as that of the variable, and can only be a constant or literal constant.
- When the value of the variable is equal to the value of the case statement, the statement after the case statement starts to execute, and the switch statement will not jump out until the break statement appears.
- When a break statement is encountered, the switch statement terminates. The program jumps to the statement execution after the switch statement. Case statements do not have to contain break statements. If no break statement appears, the program will continue to execute the next case statement until the break statement appears.
- A switch statement can contain a default branch, which is generally the last branch of the switch statement (it can be anywhere, but it is recommended to be in the last branch). Default is executed when the value of no case statement is equal to the value of the variable. The default branch does not require a break statement.
When a switch case is executed, it must be matched first. If the matching is successful, the value of the current case will be returned, and then judge whether to continue to output or jump out according to whether there is a break.
-
Code example
public static void main(String args[]){ //char grade = args[0].charAt(0); char grade = 'C'; switch(grade) { case 'A' : System.out.println("excellent"); break; case 'B' : case 'C' : System.out.println("good"); break; case 'D' : System.out.println("pass"); break; case 'F' : System.out.println("You need to work harder"); break; default : System.out.println("Unknown level"); } System.out.println("What is your grade " + grade); }
result:
Note: if there is no break statement in the case statement block, the JVM will not output the return value corresponding to each case in sequence, but continue to match. If the matching is unsuccessful, the default case will be returned. And if there is no break statement in the case statement block, after the match is successful, the values of all subsequent cases will be output from the current case.
-
graphic
2, Circular statement
1. while loop statement
-
grammar
while(Conditional expression){ Execute statement }
-
Execution sequence
When the return value of the conditional expression is true, execute the statement in "{}". After executing the statement in "{}", re judge the return value of the conditional expression. When you know that the result returned by the expression is false, exit the loop.
-
Code example
public class GetSum{ public static void main(String[] args){ int x = 1; int sum = 0 ; while(x<=10){ sum = sum + x; x++; } System.out.println("sum = "+sum); } }
result:
-
flow chart
2. do... while loop statement
-
grammar
do{ Execute statement } while(Conditional expression statement);
-
Execution sequence
The do... While statement is similar to the while statement, but it is also different. The do... While statement should first enter the loop body and then make a judgment. If it is judged as true, it will continue the loop and jump out if it is false. Therefore, do... While should be executed at least once
-
Code example
public class TestDoWhile{ public static void mian(String[] args){ int x = 10; do{ System.out.print("value of x : " + x ); x++; System.out.print("\n"); }while( x < 20 ); } }
result:
-
graphic
4. for loop statement
-
grammar
for(initialization; Boolean expression; to update) { //Code statement } //Java enhanced for loop //It is mainly used for the enhanced for loop of array. for(Declaration statement : expression) { //Code sentence } //Declaration statement: declare a new local variable. The type of the variable must match the type of the array element. Its scope is limited to the circular statement block, and its value is equal to the value of the array element at this time. //Expression: an expression is the name of the array to be accessed, or a method whose return value is an array.
-
explain
The for loop has the following instructions:
- Perform the initialization step first. You can declare a type, but you can initialize one or more loop control variables or empty statements.
- Then, the value of the Boolean expression is detected. If true, the loop body is executed. If false, the loop terminates and starts executing the statement after the loop body.
- After executing a loop, update the loop control variable.
- Detect the Boolean expression again. Loop through the above procedure.
-
Code example
public class TestFor { public static void main(String args[]) { for(int x = 10; x < 20; x = x+1) { System.out.print("value of x : " + x ); System.out.print("\n"); } } }
result
-
Illustration
Note: infinite cycle
for(;;){ ..... }
3, Cycle control
1,break
reak is mainly used in loop statements or switch statements to jump out of the whole statement block.
break jumps out of the innermost loop and continues to execute the following statements of the loop.
grammar
The usage of break is very simple. It is a statement in the loop structure:
break;
example
Test.java file code:
public class Test { public static void main(String args[]) { int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ) { // Jump out of loop when x equals 30 if( x == 30 ) { break; } System.out.print( x ); System.out.print("\n"); } } }
The compilation and operation results of the above examples are as follows:
10 20
2,continue
continue applies to any loop control structure. The function is to make the program jump to the iteration of the next cycle immediately.
In the for loop, the continue statement causes the program to immediately jump to the update statement.
In the while or do... While loop, the program immediately jumps to the judgment statement of Boolean expression.
grammar
continue is a simple statement in the loop body:
continue;
example
Test.java file code:
public class Test { public static void main(String args[]) { int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ) { if( x == 30 ) { continue; } System.out.print( x ); System.out.print("\n"); } } }
The compilation and operation results of the above examples are as follows:
10 20 40 50
3. Tag name
If you want to jump or retreat to a specific loop body in a multi-layer loop, you can use a loop with a signature
Syntax:
Tag name: loop body{ break Tag name; } Tag name: loop body{ continue Tag name; }
Reference documents:
Rookie camp