Java Looping structure

Source: Internet
Author: User

I. Format of the IF statement and its execution flow (3 kinds) The first format of the 1.if statement:

if (relational expression) {

Statement body

}

Execution process:

First judge the relationship expression to see if the result is true or false

If true, the statement body is executed

If False, the statement body is not executed

________________________________

The second format of the 2.if statement:

if (relational expression) {

Statement body 1;

} Else {

Statement body 2;

}

Execution process:

First judge the relationship expression to see if the result is true or false

If true, executes the statement body 1

If False, executes the statement body 2

________________________________

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 relationship expression 1 to see if the result is true or false

If true, executes the statement body 1

If False, continue judging the relationship expression 2 to see if the result is true or false.

If true, executes the statement body 2

If false, continue to judge the relationship expression ... See if the result is true or false

...

If no relationship expression is true, the statement body is executed n+1

Two. Format of the switch statement and the 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, the value of the expression is calculated

Second, and the case in turn, once there is a corresponding value, will execute the corresponding statement, in the course of execution, encountered a break will end.

Finally, if all the case does not match the value of the expression, the body part of the default statement is executed and the program ends.

Three. For loop format and basic use for statement format:

for (initialize statement; Judge conditional statement; Control condition statement) {

loop body statement;

}

Execution process:

A: Execute initialization statement

B: Execute the JUDGMENT condition statement to see if the result is true or false

If it is false, the loop ends.

If true, continue execution.

C: Execute loop Body statement

D: Execute control condition statement

E: Go back to B continue

Four. While loop format and basic usage (2 kinds) While loop statement format

Basic format

while (judging condition statement) {

loop body statement;

}

Extended format

initialization statements;

while (judging condition statement) {

loop body statement;

Control condition statement;

}

Review the statement format for the FOR loop:

for (initialize statement; Judge conditional statement; Control condition statement) {

loop body statement;

}

_______________________________________________

Do While loop statement format

do {

loop body statement;

}while (Judgment condition statement);

Extended format

initialization statements;

do {

loop body statement;

Control condition statement;

} while (judging conditional statements);

Execution process:

A: Execution of initialization statements;

B: Execute the Loop body statement;

C: Execute the control condition statement;

D: Execute the JUDGMENT condition statement to see if it is true or false

If true, go back to B to continue

If it's false, it's over.

Five. Three different loops (for loop, while loop, Do...while Loop)

Although the same functionality can be accomplished, there are still small differences:

The Do...while cycle executes at least one loop body.

The For loop and while loop execute the loop body only when the condition is true

Small difference between a for loop statement and a while loop statement:

Usage difference: The variable controlled by the control condition statement can no longer be accessed after the For loop is finished;

While the end of the while loop can continue to use, if you want to continue to use, use while, otherwise it is recommended for.

The reason is that the For loop ends and the variable disappears from memory, which can improve the efficiency of memory usage.

Six. Use scenarios for controlling the break of a loop statement:

In the SELECT Structure switch statement

In the Loop statement

There is no point in leaving the scene of use

The effect of break:

Jump out of a single layer loop

____________________________________________

Continue usage Scenarios:

In the Loop statement

There is no point in leaving the scene of use

The role of continue:

The single-layer loop contrasts the break and then summarizes the two differences :

Break exits the current loop and no longer executes the program

Continue exit this cycle, and continue the next cycle

Java Looping structure

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.