Branch structure (1)

if branch structure

  • The first form of if statement: if

if (relational expression) {
statement body;
}

  • Implementation process
    executed first relational expression, the result of the relationship expression is a boolean
    If the result is a true statement on the implementation of the body
    if the result is a false statement does not execute body
    finally will be executed outside the braces
    code demonstrates

```java
/*1.判断一个字符类型char的变量*/
class Demo1{
	public static void main(String[] args){
		//定义一个字符类型char的变量
		char ch='B';
		/*
		代码结构安排的明明白白的!!!
		*/
		if(ch >= 'A' && ch <= 'Z') {
			System.out.println("这里是一个大写字母!!!");
		}
		System.out.println("大括号之外的语句!!!");
	}
}
  • The second form of if statement: if else

if (relational expression) {
statement body 1;
} else {
statement body 2;
}

  • Execution flow
    First determine the relationship expression to see if the result is true or false.
    If the result is true, execute the statement body 1
    If the result is false, execute the statement body 2

For the if statement, there is a logic error that is easy to appear. This logic error is not a grammatical problem, but it is more likely to cause an error. The following program

/*错误代码演示*/
public class IfErrorTest{
	public static void main(String[] args){
		int age = 45;
		if (age > 20){
			System.out.println("青年人");
		} else if (age >40 ) {
			System.out.println("中年人");
		} else if (age > 60) {
			System.out.println("老年人");
		}
	}
}
		

Running the above program, it is found that the print result is: young people, and actually hope that 45 years old should be judged to be middle-aged people ----- there is a logic error here
because the range of age> 20 is greater than age> 45, so execute if (age> 20), the expression is correct, and then output the young people.

In order to avoid the above errors, there is a basic principle when using if ... else statements: always give priority to the narrow range of conditions. If age> 60 and age> 20 are two conditions, obviously the range of age> 60 is smaller, so the case of age> 60 should be dealt with
Correct code demo

public class IfTest{
	public static void main(String[] args){
		int age = 45;
		if (age > 60){
			System.out.println("老年人");
			
		} else if (age >40 ) {
			System.out.println("中年人");
		} else if (age > 20) {
			System.out.println("青年人");
		}
	}
}
		
  • The second form of if statement: if ... else if… else

if (judgment condition 1) {
execution statement 1;
} else if (judgment condition 2) {
execution statement 2
}

} else if (judgment condition n) {
execution statement n;
} else {
execution statement n + 1;
}

  • Implementation process
    first determines the relationship between the expression 1 to see the result is true or false
    if the statement is true on the implementation of 1
    if it is false to continue to determine the relationship between the expression 2 to see the result is true or false
    if it is true the statement is executed 2
    if it is false to continue to judge the relational expression ... to see whether the result is true or false
    ...
    if there is no relational expression is true, execute the statement body n + 1.

Code demo

/*
学生成绩的等级划分:
	90 ~ 100 优秀
	80 ~ 90 良好
	70 ~ 80 中等
	60 ~ 70 及格
	60 以下 不及格
*/
class Demo10 {
	public static void main(String[] args) {
		/*
		成绩考虑存在0.5的情况,需要使用浮点类型数据
		这里采用float类型,这里赋值一个初值
		*/
		float score = 88.0F;
		/* 成绩等级判断 */
		if (score >= 90 && score <= 100) {
			System.out.println("优秀");
		} else if (score >= 80) {
			System.out.println("良好");
		} else if (score >= 70) {
			System.out.println("中等");
		} else if (score >= 60) {
			System.out.println("及格");
		} else {
			System.out.println("不及格");
		} 
	}
}

note:
1. The condition after if, else, and else if is either a code block enclosed in curly brackets, then the entire code block is used as the conditional execution body; or the first line of the statement is terminated by a semicolon, and may even be a Empty statement (empty statement is a semicolon), then just this statement as the execution body.
2. When using if… else statements, be sure to deal with the smaller scope first.

Published 7 original articles · praised 0 · visits 103

Guess you like

Origin blog.csdn.net/qq_43634768/article/details/105564598