Java Learning Path (c) operators, control statements

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

Operator

Arithmetic operators: + 、-、 *,/,%, + + 、--。 For + +--only one variable can be manipulated, such as i++ equivalent to I=i+1, (plus after use), ++i (first to use, then the variable + 1),--usage and + +.

Assignment operators: =, + =,-=, *=,/=,%=, >>=, <<=, &=, |=, ^= have lower precedence than other operators, so the operator is often last read. Can be combined with all binocular operators to form an extended assignment operator. There can be no spaces between the two operators. The left side of the assignment operator is a variable and the right is the assigned value.

The bitwise operators are based on the 2 code notation:&, |, ~, ^, <<, >>, >>>. When the operation is to use the complement to calculate, the final result to convert to the original code. >>: When the right shift cannot be removed, the actual resulting integer is always slightly smaller than the result value of the actual operation. >>>: Unsigned Right SHIFT, 0 on the left. ^: Same as 0 XOR 1.

Relational operators: = =,! =, <, >, <=, >=.

The monocular operator (the operator required for an operation is called the monocular operator):!,~, ().

The binocular operator (the operator with the two variables required for the operation is called the binocular operator):

The trinocular operator (the operator with the required variable of three is called a trinocular operator):?:

Logical operators (operands require only Boolean values):&&, & (not shorted), | |, | (no short circuit) 、!、 ^ (returns true only if the two operands are not the same).

Process Control Statements

There are 3 types of structure in the Process Control statement:

1. Sequential structure If there is no process control, the computer always executes each row from top to bottom.

int a=0;

int b=a+1;

System.outline (b);

2. When a branch satisfies a condition, it executes the code.

Branch control: If/switch If the curly braces are omitted, the IF condition is only controlled before the first semicolon. Else itself is the condition (reverse on the IF condition).

Usage:

If (conditions are met)

{

Do something ...
}

Else

{

Do something ...
}

Switch: Executes a piece of code when it equals a value. Write the break immediately before writing the other code after each case is written;

Usage:

Switch (casing)

{

Case A:;break;//do Something A

Case B:;break;//do Something B

Default:do default Something
}

Loop control: While/do. While/for while

(1) The first execution in the judgment

do{

Do something ...

}while (An expression that returns a Boolean value)

(2) First judge in the execution

While (an expression that returns a Boolean value) {

Do something ...

}

3, loop repeats a piece of code.

For loop: Initialize statement: Initialization statement can be omitted, if any, the initialization statement is executed before the start of the loop. The judging condition can be omitted, and if omitted, it means that its value is always true. Subsequent operations can be omitted if there will be execution after each loop body execution before the next loop is started. Judging the condition returns TRUE, the program executes the loop body. Loop nesting: The memory loop is treated as a statement of the outer loop. Key words to control the loop: Break (to completely end a loop itself), continue (stop the execution of the current loop body from jumping to the next loop body), return (end the entire method). Break, continue can be followed by a label. A labeled break is used to end the loop identified by the label. The labeled continue ignores the remaining statements following the loop identified by the label. Also note that there is no Goto in Java (as a reserved word)

for (initialization statement; judging condition; subsequent operations)
{

Do something ...
}

foreach loops: such as using a Foreach loop to iterate over an array
Forach (element type variable name: Array | collection)
{
Use the read data to do something.
}

Java Learning Path (c) operators, control statements

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.