Sequence structure, judge if statement, select switch statement

Process control

Summary

In the process of a program execution, the execution order of each statement has a direct impact on the result of the program. In other words, the process of the program has a direct impact on the running results. Therefore, we must be clear about the execution process of each statement. Moreover, many times we need to control the execution order of statements to achieve the functions we need to complete.

Sequential structure

public static void main(String[] args){
//Run from top to bottom according to the order of writing
	System.out.println(1);
	System.out.println(2);
	System.out.println(3);
}

Judgement sentence

If (relation expression)
Sentence body;

  • Execution process
    • First, judge whether the result of a relational expression is true or false
    • Execute statement body if true
    • If false, the statement body will not be executed
public static void main(String[] args){
	System.out.println("start");
	// Define two variables
	int a = 10;
	int b = 20;
	//if judgment for variables
	if (a == b){
		System.out.println("a Be equal to b");
	}
	int c = 10;
	if(a == c){
		System.out.println("a Be equal to c");
	}
	System.out.println("End");

If (relation expression){
Statement body 1;
}else {
Statement body 2;
}

  • Execution process
    • First, judge whether the result of a relational expression is true or false
    • Execute statement body 1 if true
    • Execute statement body 2 if false
public static void main(String[] args){
	// Judge whether the given data is odd or even
	// Defining variables
	int a = 1;
	if(a % 2 == 0) {
		System.out.println("a Even numbers");
	} else{
		System.out.println("a It's odd.");
	}
	System.out.println("End");
}

if (judgment condition 1){
Execute statement 1;
}else if (judgment condition 2){
Execute statement 2;
}
...
}else if (judgment condition n){
Execute statement n;
} else {
Execute statement n+1;
}

  • Execution process
    • First, judge whether the result of relation expression 1 is true or false
    • Execute statement body 1 if true
    • If it is false, continue to judge the relationship expression 2 to see whether the result is true or false
    • Execute statement body 2 if true
    • If it is false, continue to judge the relationship expression See if the result is true or false
    • ...
    • If no relational expression is true, the statement body n+1 is executed.
public static void main(String[] args) {
	// The relationship between x and y is as follows:
	// x>=3 y = 2x + 1;
	//‐1<=x<3 y = 2x;
	// x<=‐1 y = 2x – 1;
	// According to the given value of x, calculate the value of y and output.
	// Defining variables
	int x = 5;
	int y;
	if (x>= 3) {
		y = 2 * x + 1;
	} else if (x >=1 && x < 3) {
		y = 2 * x;
	} else {
		y = 2 * x ‐ 1;
	}
	System.out.println("y The value is:"+y);
}

Statement exercise:

Assign test scores and judge students' grades
90-100 excellent
80-89 good
70-79 Liang
60-69 pass
Fail below 60

public static void main(String[] args) {
	int score = 100;
	if(score<0 || score>100){
		System.out.println("Your grades are wrong");
	}else if(score>=90 && score<=100){
		System.out.println("Your grades are excellent");
	}else if(score>=80 && score<90){
		System.out.println("Your grades are good");
	}else if(score>=70 && score<80){
		System.out.println("Your grades are good");
	}else if(score>=60 && score<70){
		System.out.println("Your grade is a pass");
	}else {
		System.out.println("Your grade is a failure");
	}
}}

Interchange of if statement and ternary operator

public static void main(String[] args) {
	int a = 10;
	int b = 20;
	//Define variables and save the larger values of a and b
	int c;
	if(a > b) {
		c = a;
	} else {
		c = b;
	}
	//The above functions can be rewritten in the form of ternary operators
	c = a > b ? a:b;
}

Select statement

Switch (expression){
	case constant value 1:
		Statement body 1;
		break;
	case constant value 2:
		Statement body 2;
		break;
		...
	default:
		Statement body n+1;
		break;
}
  • Execution process
    • 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, the break will end.
    • Finally, if all case s don't match the value of the expression, the body part of the default statement is executed and the program ends.
public static void main(String[] args) {
	//Define the variable and judge 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:
			System.out.println("Wednesday");
			break;
		case 4:
			System.out.println("Thursday");
			break;
		case 5:
			System.out.println("Friday");
			break;
		case 6:
			System.out.println("Saturday");
			break;
		case 7:
			System.out.println("Sunday");
			break;
		default:
			System.out.println("The number you entered is wrong");
			break;
	}
}

In the switch statement, the data type of the expression can be byte, short, int, char, enum (enumeration), and the string can be received after JDK7.

  • Penetration of case
    In the switch statement, if the break is not written after the case, there will be a penetration phenomenon, that is, it will not judge the value of the next case and run directly backward until the break is encountered, or the whole 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, since there is no break statement after case 5 is executed, the program will go back all the time, will not judge the case, will not pay attention to the break, and will directly run the whole switch.
Because of the penetration of case, beginners must write break when writing switch statements.

Published 16 original articles, won praise 11, visited 254
Private letter follow

Posted on Sun, 12 Jan 2020 06:28:45 -0500 by luitron