While,do-while,for Cycle

Source: Internet
Author: User

1, while syntax

while (condition) {

Loop body

}

Do-while syntax

do{

Loop body

}while (cyclic conditions);

For Loop Syntax

for (int i=0;i<100;i++) {

Looping operations

}

For example:

Use three methods to find out the even number within 100 and

1,int sum=0;

for (int i=1;i<=100;i++) {

if (i%2==0) {

Sum=sum+i;

}

}

System.out.println (" for 100" even and as:"+sum);

2,int sum=0;

int i=1;

while (i<=100) {

if (i%2==0) {

Sum=sum+i;

}

i++;

}

System.out.println (" even within 100" and"+sum");

3,int sum=0;

int i=1;

do{

if (i%2==0) {

Sum=sum+i;

}

i++;

}while (i<=100);

System.out.println ("The even number within the Do-while 100 is:" +sum);

2, breakpoint debugging

After debugging is started, the code line that runs to set breakpoints stops, click F6 to run the program and watch the program run

Observing variables

One-step operation can be " variables " See the current value of the variable in the view

Discover problems

fix code, rerun, fix problem

3, the purpose of the breakpoint debugging

Analysis: Find out the cause of the defect and fix the defect

4, the method of debugging breakpoints

Setting breakpoints, stepping, observing variables

5. What is a dead loop

parsing; If a loop does not have a termination condition, then the program will be executed indefinitely until we forcibly terminate the program and the program is forced to quit, so this loop, called the Dead loop.

in the process of writing our program, we must avoid the cycle of death.

6 . Use of break

When we want to terminate the execution of the entire loop during the loop, then we can consider using the break keyword

, the break keyword is generally in conjunction with the logical decision statement if.

7, the use of continue

Continue: End this cycle and continue the next cycle

Break: Jump out of the current loop {}

8. Summary of circular structure

Difference 2: Execution order

While loop: First judge, then execute

Do-while Cycle: Execute first, then Judge

for Loop: First judge, then execute

Difference 3: Where applicable

When the number of cycles is determined, the for loop is usually selected

When the number of cycles is uncertain, a while or a do-while loop is usually selected

While,do-while,for Cycle

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.