Loop structure (1), loop structure (

Source: Internet
Author: User

Loop structure (1), loop structure (

Loop Structure: The loop structure is not endless and will continue only when certain conditions are met. It is called a "loop condition ". when the cycle condition is not met, the loop exits. The loop structure is called the loop structure, which performs the same or similar operations repeatedly.

While Loop Structure

Syntax:

While (loop structure ){

// Cyclic Operation

}

Eg:

Int I = 1;

While (I <= 100 ){

System. out. println ("good learning! ");

I = I + 1;

}

The execution sequence of the while loop structure:

1. Declare and initialize the cyclic variable;

2. Determine whether the cyclic conditions are met. If the conditions are met, perform the cyclic operation. Otherwise, exit the loop;

3. After the cyclic operation is completed, judge the cyclic conditions again and decide to continue or exit the loop.

While Loop Structure Features: first judge and then execute.

The steps to solve the problem with the while loop structure are as follows:

1. Analyze the cyclic conditions and operations;

2. Use the while syntax to write the code;

3. Check whether the loop can exit.

Note: when using the while loop structure to solve the problem, be sure to check whether the loop can exit, that is, to avoid the "endless loop" phenomenon.

Common Errors:

1. No initial value is assigned to the cyclic variable;

2. {} is missing {}.

How to debug the program:

1. Analysis Error. Set the breakpoint;

2. Start debugging and perform it in one step.

Errors or defects in programs in computers are often referred to as "bugs, and program debugging is called" debug ", which means discovering and solving bugs.

Do-while loop:

When the first loop condition is not met, the while loop will not be executed once, but the do-while loop will be executed once no matter how the loop is executed, and the loop condition is determined.

Syntax:

Do {

// Cyclic Operation

} While (Cyclic condition );

Eg:

Int I = 1;

Do {

System. out. println ("good learning! ");

I ++;

} While (I <= 100 );

The execution sequence of the do-while loop is generally as follows:

1. Declare and initialize the cyclic variable;

2. Perform a loop operation;

3. Judge the cycle condition. If the cycle condition is met, the loop continues to be executed; otherwise, the loop is exited.

Do-while loop features: Execute first, at least once in the judgment. loop operation.

Do-while features the following steps to solve the problem:

1. Analyze the cyclic conditions and operations;

2. Use the do-while syntax to write the code;

3. Check the loop to exit.

Comparison between while and do-while

Similarities: they are all cyclic structures. "while (Cyclic condition)" is used to indicate cyclic conditions and braces are used to enclose cyclic operations.

Differences:

1. Syntax difference: the do-while loop structure places the while keyword and the loop condition behind, and there is a key do in front, followed

A semicolon is added;

2. the execution order is different: the while loop structure is judged first and executed; the do-while structure is executed first and the judgment is performed;

3. If the first loop condition is not met, the while loop structure will not be executed once, And the do-while loop structure will not

Under any circumstances, it should be executed at least once.

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.