Select structure_ Standard switch statement and selection structure_ Penetrated switch statement

Select statement
 
Select statement --switch
switch statement format:
switch(expression)
 { 
case Constant value 1: Statement body 1;
 break;
 case Constant value 2: 
Statement body 2; break;
 ... 
default: 
Statement body n+1; 
break; 
}
First, calculate the value of the expression
Secondly, compare with case in turn. Once there is a corresponding value, the corresponding statement will be executed. In the process of execution, a break will result in
Bundles.
Finally, if all case s do not match the value of the expression, the body of the default statement is executed, and the program ends.

 

 

public static void main(String[] args) {
 //Define variables to determine the day of the week 
int weekday = 6; 
//switch Statement implementation selection 
switch(weekday) {
 case 1:
 System.out.println("Monday");
 break;
 case 2: 
System.out.println("Tuesday"); 
break; 
case 3:
In the switch statement, the data type of the expression can be byte, short, int, char, enum (enumeration). String can be received after JDK7.
 
case penetrability
In the switch statement, if break is not written after the case, penetration will occur, that is, the value of the next case will not be judged, and it will be directly transported back
Line until a break is encountered or the overall switch ends.
public static void main(String[] args) {
 int i = 5; 
switch (i){
 case 0:
 System.out.println("implement case0"); 
break;
 case 5:
 System.out.println("implement case5");
 case 10:
System.out.println("implement case10"); 
default:
 System.out.println("implement default"); 
} 
}
In the above program, after case5 is executed, because there is no break statement, the program will always go backward. It will not judge the case, nor will it care about the break, and directly
Run the full switch.
Because case has penetrability, beginners must write break when writing switch statements.

Precautions for using switch statements:
1. the value after multiple case s cannot be repeated.
2. only the following data types can be found in the parentheses after the switch: basic data type: byte/short/char/int
Reference data type: String string, enum
3. the switch statement format can be flexible: the sequence can be reversed, and the break statement can be omitted. "The matching case will be executed downward from which position until a break or the overall end is encountered."

 
Loop statement
Cycle overview
A loop statement can repeatedly execute a piece of code when the loop conditions are met. This repeatedly executed code is called a loop body statement
When executing this loop body, you need to change the loop judgment condition to false when appropriate, so as to end the loop. Otherwise, the loop will continue to be executed, and
In an endless cycle.
Loop statement 1--for
for loop statement format:
for(Initialization expression①; Boolean expression②; Step expression④){ 
        Circulatory body③ 
}            
Execution process
Execution sequence: ① ② ③ ④ > ② ③ ④ > ② ③ ④... ② not satisfied.
① Responsible for completing the initialization of loop variables
② Be responsible for judging whether the cycle conditions are met, and jump out of the cycle if not
③ Executed statement
④ Change of variables involved in the cycle condition after the cycle

 

public class demo02 {
    public static void main(String[] args) {
        //Console output 10 times HelloWorld,Do not use cycles
        System.out.println("HelloWorld");
        System.out.println("HelloWorld");
        System.out.println("HelloWorld");
        System.out.println("HelloWorld");
        System.out.println("HelloWorld");
        System.out.println("HelloWorld");
        System.out.println("HelloWorld");
        System.out.println("HelloWorld");
        System.out.println("HelloWorld");
        System.out.println("HelloWorld");
        System.out.println("‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐");
        for(int x = 0; x < 10; x++)
        { System.out.println("HelloWorld"+x);
        }
    }
}

The basic components of the circular structure can be generally divided into four parts: the basic components of the circular structure can be generally divided into four parts:
1. initialization statement: it is executed at the beginning of the loop and only once. 1. initialization statement: it is executed initially at the beginning of the loop and only once
2. condition judgment: If yes, the cycle continues; If not, the loop exits. 3. loop body: repeat the contents of things to be done and several lines of statements. 2. condition judgment: If yes, the cycle continues; If not, the loop exits. 3. loop body: repeat the contents of things to be done and several lines of statements.
4. step statement: the finishing work to be performed after each cycle. It must be executed once after the end of each cycle. Each step statement: the finishing work to be performed after each cycle. It must be executed once after the end of each cycle

 

Posted by vbmurray on Thu, 30 Jun 2022 16:48:25 +0300