Java if else statements
Conditional statements are a type of statements that are selected and executed based on the conditions. In actual use, the difficulty lies in how to accurately abstract conditions. For example, if the user name and password are correct during program logon, the system is displayed. Otherwise, a prompt box such as "Incorrect password" is displayed.
This section introduces conditional statements, focusing on syntax explanation and basic usage. For more information, see the following comprehensive examples.
In Java, conditional statements mainly have two types of syntax: if statement and switch statement.
1. if statement
The Chinese meaning of the if keyword is that, in terms of detailed syntax induction, there are three types: if statement, if-else statement, and if-else statement. Http://hovertree.com/menu/java/
1.1 if statement
The syntax format of this type of statement is: if (conditional expression)
Function Code; syntax description: if is the keyword in the statement, followed by a pair of parentheses. This pair of parentheses cannot be omitted at any time. Inside the parentheses are specific conditions, syntax requires that the expression result be of the boolean type. The code that follows the function, that is, the code that is executed when the condition is set. When writing a program, the code needs to be indented to express the inclusion relationship intuitively.
Note the following:
1. The function code here can only be one line. The function code for multi-line structure will be described later.
2. if (condition expression) generally does not write a semicolon in the future. The execution process of the if statement is: if the condition expression is true, the function code is executed. if the condition expression is not true, then, the subsequent functional 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 the sample 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 an even number, if it is true, it is also output.
Note the following code execution process:
Int m = 20;
If (m> 20)
M + = 20;
System. out. println (m );
According to the preceding syntax format, only m + = 20. This line of code is a functional code, and subsequent output statements form an ordered structure with the preceding conditions, therefore, the output result after the program is executed is 20. if the statement to be executed has multiple sentences when the condition is set, you can use the statement block to describe it. The syntax format is as follows:
If (conditional expression ){
Function code block;
}
In this syntax format, a code block is used to replace the previous functional code. In this way, you can write any number of lines of code within the code block, and make the logic of the entire program clearer, therefore, this logic is recommended in actual code writing.
1.2 if-else clause
If-else statements implement closed conditions, which are more common in programs. 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 preceding part is the same as the if statement. The else part is followed by the functional Code. According to the syntax format, the functional code can only have one sentence.
Execution sequence: if the condition is true, run Function Code 1 in the if statement; otherwise, run the function code in else. 2. The sample 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 is not true, the code of the else statement is executed and the program outputs "n is not an odd number ".
In actual use, to make the structure clear and write multiple lines of code in the functional code section, the syntax format is:
If (conditional expression ){
Function Code Block
} Else {
Function Code Block
}
When the program contains multiple if statements, the else statement matches the nearest if statement. Sample Code:
If (condition 1)
Function Code 1;
If (condition 2)
Function Code 2;
Else Function Code 3;
Then, the else statement matches the if statement corresponding to condition 2. The preceding condition 1 is an independent statement. In actual code, braces can be used to make the entire program structure clearer.
For an if-else statement, because the if condition and else condition are mutually exclusive, only the functional code in one statement can be executed in actual execution.
In actual development, some companies require to write else even if the else statement does not write code, so that the condition can be closed. This is not a syntax requirement.
1.3 if-else
In reality, if-else statements are not a set of related conditions, such as converting Arabic numerals to Chinese uppercase and converting scores to corresponding levels, all are multi-condition structures. To avoid writing the structure of multiple if statements, a special multi-branch statement is provided. This is the 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 two keywords: else and if. Use spaces to separate them.
2. conditions 1 to n are of the boolean type.
3. The else if statement can have any number
4. The final else statement is optional.
5. If the function code is not a statement block, that is, you can only write one sentence without braces.
Execution Process: When condition 1 is set, Function Code 1 is executed. When condition 1 is set and condition 2 is set, Function Code 2 is executed; if neither condition 1 nor condition 2 is true and Condition 3 is true, execute Function Code 3, and so on. If all conditions are not true, execute the function code of the else statement. The flowchart of the execution process is shown above.
The following is an example of how to output the number of days in a month based on the value of the month, and 28 for all in January without considering the leap year:
Int month = 3; int days = 0; // 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);/* He asked hovertree.com */
Let's take A look at the sample code. The function of this Code is to convert the percentage score to 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');}/* He asked hovertree.com */
It can be seen from the code that each else if statement is written in sequence. In actual writing, it must be written in a logical order; otherwise, a logical error may occur.
The if-else statement is a multi-branch condition statement provided in Java, but it is difficult to write when determining some problems, therefore, the Syntax provides another statement, the switch statement, to better identify multiple branch statements.
Recommended: http://www.cnblogs.com/roucheng/p/3504465.html