Java Foundation-java Process Control

Source: Internet
Author: User
Tags bitwise logical operators

Iscoder Tags: Java selection structure, Java looping structure
Java Process Control
  • Operator
  • Implicit and Explicit conversions
  • Select structure
  • Loop structure
  • Loop End Control
I. Java operator 1.1 basic operator

Basic operators, including arithmetic: Add/subtract/multiply/divide. Additionally, the remainder operator% is included.

1.2 Composite operators

A composite operator, which is an operator that combines a basic operator with an assignment operator. Include: +=,-=,*=,/+,%=, it is worth pointing out that the composite operator implicitly enforces type conversions.

1.3 Logical operators

The logical operator consists of 6 operators, logic and &, logical OR |, logical not!, logical xor ^, double with &&, double or | | |. Among them, the need to pay attention to is,&& and | | and & and | The expression results are consistent, but && and | | There is an attribute that, if the result of the left expression can determine the result of the entire expression, then the expression on the right will not be judged, raising the efficiency of a drop.

1.4-bit operators

bitwise operator, with bitwise inverse ~, bitwise XOR ^, bitwise and &, bitwise OR |, bitwise left SHIFT <<, bitwise right SHIFT >>, bitwise unsigned Right shift >>>, where bitwise XOR or very characteristic, one data to another data bit XOR or even several times or itself , such as 1^2^2=1, which can be used to exchange values of 2 variables without borrowing a third-party variable.

1.5 Ternary operators

The ternary operator, also called the conditional operator. Using the ternary operator, you can quickly figure out the size of 2 numbers, which is often used to judge the big one in 2 numbers.

1.6 Self-increment/decrement operator

The self-increment and decrement operators are efficient in the low-level operation, and are often used in the condition control statements in the loop and selection judgment. The self-increment operator is divided into prefixes and suffixes, and there are 2 ways to make a single statement without any difference; in other cases, you need to know that the prefix means to participate in the increment or decrement operation. Then participate in the expression operation, the second suffix is the opposite, the value of the variable is taken to participate in the expression of the operation, and then the variable self-increment self-reduction operation.

Two. Implicit and explicit conversion 2.1 Java automatic type promotion (implicit conversion)

At the bottom of the computer, the type of the uniform variable is required for the related operation. This requires that, in a hybrid operation, the compiler can automatically convert various data types to the largest data types for unification, in Java the Automatic ascending order of the basic data types is:byte,char,short-> Int->long->float->double.

2.2 Forced-type conversions for Java (Explicit conversions)

Sometimes, you need to convert data from other data types to the required data types, such as decimal rounding. If you do not make explicit conversions, the compiler will consider the logic is not rigorous, direct compilation failure, from this point of view, the Java language than the C language of the programmer's requirements are more stringent.

Three. Select Structure 3.1 If selection structure

If, that is, if the meaning of the condition is judged. If it can change the process of the program, there are three main forms, the reasonable choice of different if forms for different situations can greatly improve the robustness of the program.

Format 1:if

Format 2:if...else ...

Format 3:if...else if...else If...else ...

The IF statement is often used when judging intervals or ranges ...

3.2 Switch Multi-branch selection

Switch, multi-branch SELECT statement, sometimes complex, there are multiple flows, then you need to use the switch () statement for Process Control.

Switch (byte/short/char/int/string/enumeration) {

Case Scenario 1: ...; Break

Case Scenario 2: ...; Break

... ...

Default: ...; Break

}

Switch is often used to determine the use of fixed values ...

Four. Loop structure 4.1 for loop

A For loop, often used for loops where cycles are known, such as a loop that prints 1-10 of the number. Looping is one of the most important structures in programming languages, and loops have a great advantage in reducing the amount of code that can be reused. The For loop has the following 2 formats, the first of which is the general form, the 2nd type, is the iterator loop for each, which is used primarily to iterate over the collection elements.

Form 1:

For (initialization expression 1; conditional judgment expression 2; loop control expression 3) {

Circulation body;

}

Form 2:

For (data type variable name: Collection) {

Circulation body;

}

4.2 While loop

The while loop, in contrast to a for loop, is used for an unknown number of occasions, such as printing an element in an array until the number 0.while loop is encountered with only one form, as follows:

initialization expression 1;

while (conditional judgment expression 2) {

Circulation body;

loop control expression 3;

}

4.3 Do...while cycle

Do...while cycle, use very little. is a type of loop, and the while loop can be replaced with equivalent. There is only one form, as follows:

initialization expression 1;

do{

Circulation body;

loop control expression 3;

}while (conditional-judgment expression 2);

Five. Loop end Control 5.1 break keyword

The BREAK keyword has 2 applications: loop structure and switch multi-branch selection structure. Break, mainly used to jump out of the loop, it is worth noting that the break can only jump out of a loop at a time, with a "label" break will jump to the outermost loop, regardless of how many loops nested, But not commonly used.

5.2 Continue Keywords

The Continue keyword can only be used in a looping structure. Continue can only end this cycle, not jump out of the loop, in the final analysis continue can not really end the loop. The most important point of break and continue is that there is no valid statement under it. Because it is never possible to execute the following statements to break and continue, the compiler considers the situation to be not rigorous and the direct compilation fails.

5.3 Return keyword

In fact, return can also jump out of the loop, but it is more powerful than break, directly terminates the execution of the method. It should be noted that any method has a return statement (some users explicitly give, and the user does not explicitly give the system will automatically add return;), Because this is guaranteed to return the caller out of the premise of continuing to execute other statements.

Java Foundation-java Process Control

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.