Comparison of three cycles

Source: Internet
Author: User

 

Switch-case

Syntax:

Switch (expression/variable)

{

Case value 1: Statement 1;

Break;

Case value 2: Statement 2;

Break;

Default: Statement 3

Break;

.............................

}

 

Execution Process

 

WhenProgramRun switch (expression/variable) to judge the expression in brackets first,

 

Execute the value 1 after case in braces: If the value matches 1, execute Statement 1,

Run break, jump out of the switch, and then executeCode

If the value 1 does not match, start to judge the value 2,

Match or not. If yes, Statement 2 is executed and break is executed. The switch is jumped out,

If none of them match, run the code after default and then break.

 

 

While Loop

 

 

Syntax:

 

Int I = 0;

While (condition/expression) --------------------------------- à I <a certain number

{

Loop body;

I ++;

}

 

Execution Process:

 

When running the while (condition) program, you must first determine whether the condition is true.

If true is true, the loop body is executed if false is not true,

And I ++ (I value plus 1)

If not, skip the code after the loop execution.

 

While, you must first determine whether the condition is true and then execute the loop body,

Do-while first executes the loop body and then judges

 

While loop example:

 

 

Do

{

Loop body;

} While (condition );

Execution Process:

 

When the program runs to do, it directly enters the loop body;

Then, go to the while (condition) line of code to determine.

If the condition is true, execute the loop body again,

Otherwise, jump out of the loop and execute the subsequent code;

 

At least one loop body is executed, (Do-while)

It is possible that when the initial value of the loop body (while) does not meet the conditions

 

 

Do-while first executes the loop body and then judges

While, you must first determine whether the condition is true and then execute the loop body,

 

Example of do-while:

 

 

Comparison between a while loop and a do-while loop

 

 

 

For Loop

 

For (expression 1; expression 2; expression 3)

{

Loop body;

}

 

Execution Process:

 

 

When the program runs for (expression 1; expression 2; expression 3)

This line of code

 

First run to: expression 1

Run expression 2 again

 

The loop body in the execution;

Execution expression 3;

Execute expression 2 again

Then execute the loop body;

 

For Loop Example 1:

 

 

 

For Loop Example 2: (you can use F11 for step-by-step testing)

 

 

Comparison of while and for loops:

 

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.