Java loop structure, java Loop

Source: Internet
Author: User

Java loop structure, java Loop
1. If statement format and execution process (3) 1. if statement first format:

If (relational expression ){

Statement body

}

Execution Process:

First, determine whether the relational expression is true or false.

Execute the statement body if it is true.

If it is false, the statement body is not executed.

________________________________

2. The second format of the if statement:

If (relational expression ){

Statement body 1;

}Else {

Statement body 2;

}

Execution Process:

First, determine whether the relational expression is true or false.

If true, statement body 1 is executed.

If it is false, the statement body 2 is executed.

________________________________

The third format of the if statement:

If (relational expression 1 ){

Statement body 1;

}Else if (Relational Expression 2 ){

Statement body 2;

}

...

Else {

Statement body n + 1;

}

Execution Process:

First, judge the relational expression 1 to see whether the result is true or false.

If true, statement body 1 is executed.

If it is false, continue to judge the relational expression 2 and check whether the result is true or false.

If true, statement body 2 is executed.

If it is false, continue to judge the relational expression... Check whether the result is true or false.

...

If no relational expression is true, execute the statement body n + 1.

 

Ii. switch statement format and execution process switch statement format:

Switch (expression ){

Case value 1:

Statement body 1;

Break;

Case value 2:

Statement body 2;

Break;

...

Default:

Statement body n + 1;

Break;

}

Execution Process:

First, calculate the expression value.

Second, compare it with case in sequence. Once a corresponding value exists, the corresponding statement is executed, and break is executed.

Finally, if all the cases do not match the expression value, the system executes the part of the default statement and ends the program.

 

III. for Loop format and basic use of for statement format:

For (initialization statement; judgment Condition Statement; Control Condition Statement ){

Loop body statement;

}

Execution Process:

A: Execute the initialization statement.

B: execute the judgment Condition Statement to check whether the result is true or false.

If it is false, the loop ends.

If it is true, continue to execute.

C: Execute the loop body statement.

D: Execute the Control Condition Statement.

E: Return to B to continue

 

Iv. while loop format and basic usage (2) while loop statement format

Basic Format

While (Condition Statement ){

Loop body statement;

}

Extended format

Initialization statement;

While (Condition Statement ){

Loop body statement;

Control Condition Statement;

}

Review the statement format of the for Loop:

For (initialization statement; judgment Condition Statement; Control Condition Statement ){

Loop body statement;

}

_______________________________________________

Do while LOOP statement format

Do {

Loop body statement;

} While (Condition Statement );

Extended format

Initialization statement;

Do {

Loop body statement;

Control Condition Statement;

} While (Condition Statement );

Execution Process:

A: Execute the initialization statement;

B: Execute the loop body statement;

C: Execute the Control Condition Statement;

D: execute the judgment Condition Statement to check whether it is true or false.

If it is true, return to B to continue

If it is false, it ends.

V. Differences between the three types of loops (for loop, while loop, do... while loop)

Although the same functions can be completed, there are a few differences:

Do... The while LOOP executes the body at least once.

The for loop and the while loop will be executed only when the condition is set.

A small difference between a for loop statement and a while loop statement:

Usage difference: the variable controlled by the Control Condition statement cannot be accessed after the for loop ends;

The while loop can still be used after it ends. If you want to continue using it, you can use while. Otherwise, we recommend that you use.

The reason is that the for loop ends and the variable disappears from the memory, which improves the memory usage efficiency.

 

6. Use Cases of control loop statement break:

In the select structure switch statement

In a loop statement

It is meaningless to leave the application scenario.

Roles of break:

Jump out of a single loop

____________________________________________

Use Cases of continue:

In a loop statement

It is meaningless to leave the application scenario.

The role of continue:

Compare the single-layer loop with break, and then summarize the twoDifferences:

The break exits the current loop and no longer executes the program.

Continue to exit this loop and continue the next loop

 

 

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.