Conditional statements are a type of statements in the program that select and execute based on whether the conditions are true. In actual use of such statements, the difficulty lies in how to accurately abstract conditions. For example, when implementing the program login function, if the user name and password are correct, you will enter the system, otherwise a prompt box such as "Password error" will pop up.
The introduction of conditional statements in this part focuses on grammar explanation and basic use. For more detailed use, please refer to the subsequent comprehensive example section.
In Java language, conditional statements mainly have two types of syntax: if statement and switch statement.
1. If statement
If keyword Chinese means if. In a detailed syntax summary, there are three types: if statement, if-else statement and if-else if-else statement, will be introduced below.
1.1 if statement
The syntax format of this class statement is: if (conditional expression)
Function code; Syntax description: if is the keyword in the statement, followed by a pair of brackets. The pair of brackets cannot be omitted at any time. The internal conditions of the brackets are specific, and the syntax requires that the expression result is of boolean type. The subsequent code is function, that is, code executed when the conditions are established. When writing a program, the function code generally needs to be indented in order to intuitively express the inclusion relationship.
Special attention should be paid to:
1. The function code here can only be one line. Regarding the function code of multi-line structure, it will be explained later.
2. If (conditional expression) The execution process of the subsequent execution of if statements is not written generally is: if the conditional expression is true, the function code is executed, and if the conditional expression is not true, the subsequent function code is not executed.
Sample code:
int a = 10;
if(a >= 0)
System.out.println("a is a positive number");
if ( a % 2 == 0)
System.out.println("a is an even number");
In this example code, the first condition is to determine whether the value of variable a is greater than or equal to zero. If the condition is true, the output is executed. The second condition is to determine whether variable a is even, and if true, it is output.
Pay attention to the execution process of the following code:
int m = 20;
if (m > 20)
m += 20;
System.out.println(m);
According to the previous syntax format description, only m+=20; this line of code belongs to the functional code, and the subsequent output statements and the previous conditions form a sequential structure, so the output result after the program is executed is 20. If when the condition is true, there are multiple sentences to execute, you can use statement blocks to express it. The syntax format is as follows:
if (conditional expression) {
Function code block;
}
Using this syntax format, a code block is used instead of the previous functional code, so that as many lines of code can be written inside the code block, and the logic of the entire program is clearer. Therefore, this kind of logic is recommended in actual code writing.
1.2 if-else statement
The if-else statement implements closed conditions and is more common in programs. The function of the else keyword is "otherwise", that is, the condition is not true.
The syntax format of the if-else statement is as follows:
if (conditional expression)
Function code 1;
else
Function code 2;
Syntax description: The previous part is the same as the if statement, and the else part is followed by the function code. According to the syntax format, the function code can only have one sentence.
Execution order: If the condition is true, execute function code 1 in the if statement, otherwise execute function code 2 in else. The example code is:
int n = 12;
if(n % 2 != 0)
System.out.println("n is an odd number");
else System.out.println("n is not an odd number");
Then because the value of n%2 is 0 and the condition does not hold, the code of the else statement is executed and the program outputs "n is not an odd number".
In actual use, in order to make the structure clear and multiple lines of code can be written in the functional code part, the functional code part is generally used with code blocks, and the syntax format is:
if (conditional expression) {
Function code block
}else{
Function code block
}
When there are multiple ifs in the program, the else statement matches the most recent if. Sample code:
if (condition 1)
Function code 1;
if (condition 2)
Function code 2;
else function code 3;
Then the else statement here matches the if statement corresponding to condition 2, and the previous condition 1 is an independent statement. In actual code, curly braces can be used to make the structure of the entire program clearer.
For if-else statements, because the conditions of if and else are mutually exclusive, in actual execution, only the functional code in one statement will be executed.
In actual development, some companies require that they must write else when writing conditions even if they do not write code in the else statement, so that the conditions can be closed. This is not a syntax required.
1.3 if-else
If-else statement In reality, sometimes the conditions are not one, but a set of related conditions. For example, converting Arabic numerals to Chinese capital and converting them to corresponding levels according to fractions are all multi-conditional structures. In order to avoid writing multiple if statements in the program, a special multi-branch statement is provided, which is the if-else if-else statement.
The syntax format of the if-else statement is:
if (condition 1)
Function code 1;
else if (condition 2)
Function code 2;
else if (condition 3)
Function code 3;
...
else function code;
Syntax description:
1. else if is the two keywords else and if, spaces are used in the middle.
2. Condition 1 to condition n are both boolean types
3. The else if statement can have as many sentences as possible
4. The last else statement is optional
5. If the function code part is not a statement block, that is, there is no curly braces, you can only write one sentence.
Execution process: When condition 1 is true, the function code 1 is executed; when condition 1 is false and condition 2 is true, the function code 2 is executed; if condition 1 and condition 2 are neither true and condition 3 is true, the function code 3 is executed, and so on. If all conditions are false, the function code of the else statement is executed. The flowchart of its execution process is shown above.
Here is an example code for an implementation that outputs the number of dates contained in that month based on the value of the month, and all of February outputs 28, regardless of leap years:
int month = 3; int days = 0; //Date number if(month == 1){ days = 31; }else if(month == 2){ days = 28; } else if(month == 3){ days = 31; } else if(month == 4){ days = 30; } else if(month == 5){ days = 31; } else if(month == 6){ days = 30; } else if(month == 7){ days = 31; } else if(month == 8){ days = 31; } else if(month == 9){ days = 30; } else if(month == 10){ days = 31; } else if(month == 11){ days = 30; } else if(month == 12){ days = 31; } System.out.println(days);Let’s take a look at another example code. The function of this code is to convert the percentage score into A, B, C, D and E. The code is as follows:
int score = 87; if(score >= 90){ System.out.println('A'); } else if(score >= 80){ System.out.println('B'); } else if(score >= 70){ System.out.println('C'); } else if(score >= 60){ System.out.println('D'); } else{ System.out.println('E'); }From this code, we can see that each else if statement is written in sequence. When actually writing, it must be written in logical order, otherwise logical errors will occur.
If-else if-else statement is a multi-branch conditional statement provided in the Java language, but it will be troublesome to write when judging certain problems, so another statement is provided in the syntax - the switch statement to better realize the discrimination of multi-branch statements.
The above java's if else statement introduction guide (recommended) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.