Java Third day (operator, keyboard entry, and control statement)

Source: Internet
Author: User
Tags arithmetic operators bitwise logical operators

1: operator (master)
(1) Arithmetic operators
a:+,-, *,/,%,++,--
The use of b:+
A: Addition
B: plus sign
C: String connector
The difference between c:/and%
When the data is in division operation,/get is quotient,% get is remainder
The use of d:++ and--
A: Their role is self-increment or self-reduction
B: Use
* * Used alone
Put in front and back of the action data.
The a++ or ++a effect is the same.
* * Participate in operation using
Put in front of operand: increment or decrement first, then participate in operation
int a = 10;
int b = ++a;
At the back of the operand: take part in the operation first, then self-increment or decrement
int a = 10;
int b = a++;
(2) Assignment operator
a:=,+=,-=,*=,/=,%=, etc.
B:= is called the assignment operator and is the most basic assignment operator
int x = 10; Assign 10 to the variable x of type int.
C: Features of the extended assignment operator
Implicit automatic casts.

(3) Comparison operators
a:==,!=,>,>=,<,<=
B: Regardless of whether the operator is simple or complex, the end result is a Boolean type.
C: Don't write = =
(4) Logical operators
a:&,|,^,!, &&,| |
B: Logical operators are used to concatenate Boolean types
C: Conclusion
&: False if False
|: True is true
^: The same is false, and the difference is true.
Couple relationship.
!: Not true false, not false true

&&: The result is the same as the &, except that it has a short-circuit effect. The left side is false and the right side is not executed.
| |: The result and | are the same, but with short-circuit effect. The left is true and the right side is not executed.
(5) bitwise operator (LEARN)
Special usage of a:^
One data for another data bit XOR or two times, the number is unchanged
B: Face question
A: Please implement the exchange of two variables
* * Use of third-party variables
* * with bitwise XOR OR operator
Left A,b,a
Right A^b
B: Please calculate the result of 2 times 8 in the most efficient way.
2<<3
(6) Ternary operator
A: Format
Compare Expressions 1: expression 2;
B: Execution Process:
The value of the comparison expression is evaluated first to see if it is true or false.
If true, the expression 1 is the result.
If False, the expression 2 is the result.
C: Case:
A: Compare two data for equality
B: Get the maximum value from two data
C: Get the maximum value from three data

2: Keyboard entry (Master)
(1) in the actual development, the data is changed, in order to improve the flexibility of the program, we add keyboard input data.
(2) How to achieve it? Now remember
A: Guide Package
Import Java.util.Scanner;
Position: On top of class
B: Create an Object
Scanner sc = new Scanner (system.in);
C: Get Data
int x = Sc.nextint ();
(3) Adding the case of ternary operator to keyboard input improvement.

3: Process Control statement
(1) sequential structure from top to bottom, executed sequentially
(2) Select structure to execute different codes according to different choices
(3) Loop structure to do some repetitive code

4:if Statement (Master)
(1) Three formats
A: Format 1
if (comparison expression) {
Statement body;
}

Execution process:
Judge the value of the comparison expression to see if it is true or false
If true, the statement body is executed
If False, the statement body is not executed

B: Format 2
if (comparison expression) {
Statement body 1;
}else {
Statement body 2;
}

Execution process:
Judge the value of the comparison expression to see if it is true or false
If true, the statement body is executed 1
If False, the statement body 2 is executed

C: Format 3
if (comparison expression 1) {
Statement body 1;
}else if (comparison expression 2) {
Statement body 2;
}
...
else {
Statement body n+1;
}

Execution process:
Determine the value of the comparison expression 1 to see if it is true or false
If true, the statement body is executed 1
If false, continue to judge the value of the comparison expression 2 to see if it is true or false
If true, the statement body is executed 2
If false, continue to judge the value of the comparison expression 3 to see if it is true or false
...
If all is not satisfied, execute the statement body n+1
(2) Precautions
A: Comparing expressions whether simple or complex, the result is a Boolean type
B:if statements control the body of a statement if it is a statement, you can omit the curly braces, if it is more than one, cannot be omitted.
Suggestion: Never omit.
C: Generally, there are left curly braces, there is no semicolon, there is a semicolon, there is no left curly brace.
D:else If there is no if, the comparison expression is not present.
E: Three If statements are actually a statement, as long as there is one execution, the other is no longer executed.
(3) Case:
A: Compare whether two numbers are equal
B: Get the maximum value in two numbers
C: Gets the maximum value in three digits (nested if statement)
D: According to the grade of the result output corresponding
E: According to the month, the corresponding season of the output
F: Calculates the value corresponding to Y according to X and outputs
(4) The relationship between ternary operator and the second form of if statement
All ternary operators can be implemented, and the second form of the IF statement can be implemented.
Conversely, it is not established.

If the statement body that is controlled by the second format of the IF statement is an output statement, it is not possible.
Because the ternary operator is an operator, you must have a result returned and cannot be an output statement.

Java Third day (operator, keyboard entry, and control statement)

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.