Dark Horse programmer-------Java Basics (i) Some basic program flow control in Java

Source: Internet
Author: User

-------Android Training, Java training, look forward to communicating with you! ----------

The judgment structure of if/else Constitution

if (conditional expression) {    expression 1;}Else{     expression 2;}

This means that if the conditional expression after the If is true, the expression 1 is executed, otherwise the expression 2 is executed;

Equivalent to the previously learned ternary operator structure of the judgment:

(conditional expression)? Expression 1: Expression 2

The meaning is: when the conditional expression inside the parentheses is true, the expression 1 is executed, and the operation ends;

When the conditional expression in parentheses has a logical value of false, the expression 2 is executed and the operation ends.

Note: The advantage of the ternary operator is that it simplifies the If/else code, but there are drawbacks, it is just an operator, so you have to have a concrete result after the operation.

if (conditional expression 1) {    expression 1;}Elseif(conditional expression 2) {     expression 2;}Elseif  (conditional expression 3) {   expression 3;} ... Else {   expression n}

This structure, as long as the conditional expression is satisfied when the operation is done to a conditional expression, ends the whole operation.

II. Choice of structure

Switch selection structure

Switch (expression)//the data type of an expression can be only a byte, short, int, or char type{ /*default: Execute statement; When default is the most front-end of the selection result, only the case followed by the result of the value of all matching, will be executed after the default execution statement, this time must write break, if there is no break, Continues to execute downward, but does not match the value of the case followed by executing each subsequent execution statement until it encounters a break or a stop. */   CaseTake value 1: Execute statement 1; Break; CaseTake value 2: Execute statement 2; Break; ......default: Execute statement;//Execute the default execution statement followed by defaults only if the above answer is all wrong    Break;//Break is optional at this time}

Note: Swtich You cannot determine the condition that the result is a Boolean type.

When the specific data (byte, short, int or char type) is judged, and the value is not long, you can use the Swtich structure;

If the value is a range or Boolean type, the IF structure can be used, if the structure of a wider scope of application.

Third, the cycle structure

(i) The loop structure of the while and the Do/while

1)

 while (conditional expression) {        loop body (execution statement);// If there is no control condition in the loop body, the contents of the loop body are executed. }

2)

 Do {      loop body;}   while(conditional expression)// If the condition is satisfied, at least once to perform the loop body behind do. 

Difference:

A:while the condition, the content of the loop body is executed only when the condition is satisfied.

B:do/while executes the contents of the loop body first, and then resumes the loop if the condition is met.

(ii) for loop

 for (initialization expression; cyclic conditional expression; post-loop action expression)         {                   execute statement;         }

The process of a For loop

The difference between the for loop structure and the while loop structure:

1) If the variable is limited to the existence of a loop increment within the loop, and is used to control the number of cycles, the for loop structure, the statement is simple, can optimize the memory.

2) The variable defined in the For loop structure is used only within the loop structure, and after the loop is executed, the variable is automatically freed in memory, while the variables in the while loop structure are usually defined outside the loop structure and still exist after the loop has ended

Extension: The simplest form of expression for infinite loops

for (;;)

{

}

while (true)

{

}

Note: It is important to make sure which statements need to be involved in the loop and which do not need

The idea of accumulating in a loop: recording the result of a cyclic operation by a variable

1) record the results of each change

2) Cumulative action through the loop

(iii) Expansion: Added new features for advanced for loops in the JDK1.5 version

Format:

 for (Variable type variable: the collection or array to be traversed)        {                   loop body;         }

The underlying principle of the advanced for loop is the iterator iterator, which iterates through the collection and can only get the elements and cannot manipulate the collection of elements.

The difference from the normal for loop: the advanced for Loop has a limitation that there must be a traversal target, for example:

If a statement is printed in a loop, you can control the number of times by using the normal for loop, but you cannot use the advanced for loop operation

for (int. x=0;x<100;x++) {         System.out.println ("Hello World");}

In this case, it is not possible to use the advanced for loop because the for loop is only controlling the number of loops and there is no target collection or array to be traversed;

It is recommended that you use a traditional for loop when iterating through an array, because the traditional for loop can also operate on an array of arrays based on the acquisition of a set angle.

Four, in the loop structure, we usually use break (jump) and continue (continue) operation, the following summarizes the use of the two methods:

Break the function of the statement

(1) The break statement can be used only within the body of the loop and in the switch statement.
(2) When break appears in the body of the switch statement in the loop body, its function is simply to jump out of the switch statement body.
(3) When break appears in the loop body, but not in the switch statement body, then after the break, the body of the loop is out of the layer.

(4) In the loop structure, the break statement is applied to make the process jump out of the loop body of this layer, thus ending this layer loop prematurely.

Continue statement function

(1) The general form of the continue statement is: continue;

(2) Its function is to end this cycle, that is, the remainder of the loop is skipped the remaining outstanding statements, and then once again the condition of the loop to determine.
(3) Note: Executing the CONTINUE statement does not terminate the entire loop. In the while and do-while loops, the Continue statement causes the process to jump directly to the test portion of the loop control condition, and then decides whether the loop continues.

(4) in the For loop, after encountering continue, skips the remaining statements in the loop body, evaluates the "After loop action expression" in the For statement, then tests the condition of the "loop condition expression", and finally determines whether the for loop executes based on the value of the "loop condition expression". In the loop body, regardless of the statement component in which the continue is used, it is performed as described above, which differs from break.

Dark Horse programmer-------Java Basics (i) Some basic program flow control in Java

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.