Chapter 3 operators and condition structures, Chapter 3 Operators
1. boolean: true or false
True false
2. Accept input information from the console and receive it by creating a scanner
Wrote input = new partition (System. in );
Int inout = input. nextInt (): // receives the int type.
String name = inout. next (); // receives the String type
3. What is the difference between = and =?
1) "=" is the value assignment operator, which assigns the value on the right to the variable on the left of "=. For example, int num = 20;
2) "=" is a comparison operator, that is, the value on the left of "=" is compared with the value on the right to see if they are equal. If they are equal, true is used; otherwise, false is used; for example, the result of 3 = 4 is false;
4. Operator priority
Arithmetic Operators> Relational operators> logical operators
5. if select Structure
If (expression ){
Statement
}
Execute the statement when the expression is true.
6. common logical operators
& And, if both conditions are true, the result is true. If either of the conditions is false, the result is false.
| If either of the conditions is true, the result is true. If both conditions are false, the result is false.
! If the non-condition is true, the result is false. Otherwise, the result is true.
7. The operators are classified into single object operators, binary operators, and three object operators.
Single Object OPERATOR :! ++ --
Binary OPERATOR: = +-* % & | =
Three-object OPERATOR :? For example, int sign = java> 10? True: false;
8. if-else Structure
If (Table-based ){
Statement 1;
} Else {
Statement 2;
}
If the expression is true, execute Statement 1. Otherwise, execute Statement 2.
9. Multi-if Structure
If (expression 1 ){
Statement 1;
} Else if (expression 2 ){
Statement 2;
} Else {
Statement 3
}
10. nested if selection Structure
If (condition 1 ){
If (condition 2 ){
Statement 1;
} Else {
Statement 2
}
} Else {
Statement 3
}
10. switch select Structure
Switch (){
Case value 1:
Statement 1;
Break;
Default:
Statement 2;
Break;
}
11. The switch structure has the following rules:
1) The values in the case clause must be constants, and the values in all case clauses should be different.
2) the return value of an expression must be of one of the following types: char byte short int and must be a definite result.
3) The default clause is optional.
4) The break statement is used to make the program jump out of the switch statement quickly after a case Branch is executed. If the break is not written after the case, it is executed directly below.
12. Comparison between switch and multi-if
Similarities: multiple branch structures can be implemented.
Differences: 1. The switch structure can only process equivalent condition judgment, and the condition is equivalent judgment of integer or character variable;
2. Multi-if judgment interval value, switch judgment equivalent