JavaSE - Operators and Flow Control

 

1. Operator

  1. Arithmetic operators:

    (1) When performing mixed operations: byte, short, and char will not be converted to each other, and will be automatically promoted to int type. When other types perform mixed operations, small data types are promoted to large ones: byte, short, char-int-long-float--double,

        float is larger than long:

        

        

    (2) An exception is generated when an integer is divided by 0, and a floating-point number is divided by 0 to get infinity or NaN

    (3) Self-addition (++), self-subtraction (--)

Int b=++a;//equivalent, a=a+1 and then assign it to b

Int b=a++;//equivalent, b=a and then calculate a=a+1

(4) Left addition (+=), left subtraction (-=), left division (/=), left modulo (%=)

  2. Relational operators: && (logical and), || (logical or), ! (No)

  3. Ternary operator: Ternary operator: Conditional expression? Expression 1: Expression 2 //When the conditional expression is true, execute expression 1, otherwise execute expression 2

  4. Operation level determination: Right associative operation is performed from right to left, a+=b+=c, which is equivalent to a+=(b+=c); && has a higher priority than ||

2. Process control

  1. Sequence control: from top to bottom, from left to right

  2. Selection control: single branch, double branch, multi branch

    (1) if statement:

       In the If judgment statement, the curly brackets at the outer layer of the statement body can be omitted; in multiple if statements, if a certain condition is satisfied, the control is immediately exited, even if there are satisfied judgment statements below. different from switch

    (2) Switch statement:

      The data types that can be used in Switch are mainly: byte, short, int, char, enum (enumeration), and String can be used after version 1.7. And the case can only be followed by constants or constant expressions, and variables cannot be placed (even if the variables are known)

      

      After running, 1 and 2 will be printed; if i=4, default and 0 will be printed

  3. Cycle control:

    (1) If there is only one statement in the loop body, the outermost curly braces can be omitted, but not multiple ones

       Int i,j; is a valid compilation

       For (int i=5,j=1;i<...) is correct

       For (i=5, int j=1;i<…) is wrong

       Int i,j

       For (i=5, j=1;i<…) is correct

(2) Common infinite loop

While(true)

{

}

For( ; ;)

{

}

    (3) The difference between break, continue, and return, (continue is to terminate the current cycle and start the next cycle)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325240238&siteId=291194637