Today’s sharing started, please give us more advice ~

Today, I will share with you the details of the operator and the if statement. This is the basic content. It is very important to lay a good foundation.

Operator

1. Arithmetic operators

  • + addition
  • – subtraction
  • * the multiplication
  • / Division (integer division, only integer results. To get a decimal, you have to have a decimal.)
  • % more than take

  • + + since
  • – the decrement

Used alone: ++ and – before and after variables have the same effect. It’s all about increasing or decreasing the variable

Participate in operation:

If ++ or – precedes a variable, it is incremented or subtracted first and then assigned

If ++ or – is placed after a variable, the original value is used first. And then it increases or decreases

The sample code

2. Characters participate in the + sign operation

  • When a character participates in an operation, the corresponding code table value is used for the operation
  • ‘A’ 65 ‘A’ 97 ‘0’ 48
  • Data type conversion rules when different data types participate in operations

Short, char->int->long->float->double

The sample code

3. The string participates in the + sign operation

  • A string concatenated with a + sign to any data type forms a new string
  • String first, do string concatenation
  • The string comes after, does the previous operation, and then concatenates it with the string

The sample code

4. The assignment operator

  • = assignment
  • += add, and then assign
  • -= subtract and assign
  • *= multiply, and then assign
  • /= divide and assign
  • %= mod, and then assign

The sample code

5. Compare (relation) operators

  • More than >
  • < <
  • The value is greater than or equal to >=
  • Less than or equal to <=
  • Equal = =
  • Is not equal to! =

The sample code

6. Logical operators

  • Logic and represents the meaning of and &
  • Logic or representative or |
  • Logical xor means same is false, different is true ^
  • The logical not! If it is not true, it is false; if it is not false, it is true
  • Short circuit with: && If the left side is false, the right side is not executed
  • Short circuit or: | | if true on the left, the right is not carried out

The sample code

7. Ternary operators

  • Define the format

(Relational expression)? Expression 1: expression 2;

  • Execute the process

Executes the relational expression and, if true, takes the value of expression 1

Executes the relational expression, taking the value of expression 2 if the result is false

The sample code

Two, keyboard input

  • Import java.util.Scanner; import java.util.
  • Create an object: Scanner sc = new Scanner(system.in);
  • Int num = sc.nextint ();

The sample code

  • Case: Keyboard input three data, get the maximum value

3. Process control statements

1. Sequential structure

  • Sequential structure is to execute in the order that we define

2. If statements

  • Define the format

  • Execute the process

Executes the relational expression or, if true, the statement body

If false, the statement body is not executed

  • Case – Determine whether or not you are an adult

  • Case – Determine if two numbers are equal

3. If – else statements

  • Define the format

  • Execute the process

Determines the value of a relational expression. If true, body 1 is executed

If false, body 2 is executed

  • Case – Determine whether the value of A is greater than b

  • Case – keyboard input number judgment is odd and even

4. If, else if statement

  • Define the format

  • Execute the process

Determine the value of relation expression 1, if true, execute statement body 1. If false

Continue to determine the value of relational expression 2, if true, execute statement body 2. If false

When all relational expressions are not satisfied, execute the body of the last else, n+1

  • Case study – Exam awards

The interview questions

  • What are the arithmetic operators?
  • What is the underlying mechanism of character participation in + sign operation?
  • Rules for the + sign operation?
  • What are the assignment operations?
  • What are the relational operators?
  • What are the logical operators?
  • What is the definition format and execution flow of a ternary operator?
  • How many steps are involved in keyboard data entry?
  • What is the definition format and execution flow of an if statement?

summary

Details determine success or failure, although the content is more basic, but for small white or very friendly. In some interviews will often ask more basic questions, we must pay attention to!

Today’s share has ended, please forgive and give advice!