The concept of conditional statements
Conditional statements can be given a determination condition, and determines whether the condition is satisfied, performs different operations according to the determination result, so as to change the order of execution of the code, the more functionality during program execution.
Conditional statements if
Single if statement:
Implementation process
- First, determine the relationship between the expression to see the result is true or false
- If the statement is true on the implementation of the body
- If false statement is not executed body
Execution Flow
demand
Illustrates the flow of execution if statement
Code
public class Demo02IfTest { public static void main (String [] args) { // definition of two variables int A = 66 ; int B = 66 ; // variables determining if, a == b is a relational expression if (A = = B) { // loop System.out.println ( "digits equal to 2" ); } // the following if condition code does not belong System.out.println ( "Code executed after the if statement" ); } }
Results of the
Standard format if ... else:
Implementation process
- First, determine the relationship between the expression to see the result is true or false
- If the statement is true on the implementation of 1
- If false statement on the implementation of 2
Execution Flow
demand
Illustrates the execution flow if else
Code
public class Demo03IfElseTest { public static void main (String [] args) { // determines the given data is odd or even // define the variable int A =. 1 ; IF (A% 2 == 0 ) { System.out.println ( "A is an even number" ); } the else { System.out.println ( "A is an odd number" ); } System.out.println ( "end" ); } }
Results of the
Extended if ... else if ... else format:
Implementation process
- First, determine 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 the statement is true on the implementation of 2
- If it is false to continue to determine the relationship between the expression ... see the result is true or false
- …
- If there is no relationship between the expression is true, then the statement is executed body n + 1.
Execution Flow
demand
Illustrate execution flow of if..else if ... else
Specifies the test scores to determine student grades 80-89 90-100 good good good 70-79 60-69 60 or less pass fail
Code
public class Demo05IfElsePractise { public static void main(String[] args) { int score = 120; if (score >= 90 && score <= 100) { System.out.println("优秀"); } else if (score >= 80 && score < 90) { System.out.println("好"); } else if (score >= 70 && score < 80) { System.out.println("良"); } else if(Score> = 60 && Score <70 ) { System.out.println ( "pass" ); } the else IF (Score> = 0 && Score <60 ) { System.out.println ( "fail" ); } the else { // separate treatment pathological case outside the boundary System.out.println ( "data error" ); } } }
Results of the
note:
In some simple applications, if the statement is a ternary operator can be used interchangeably. For example: selecting the maximum value between the digital