Chapter 3 operators and condition structures, Chapter 3 Operators

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.