A description of the difference between a for, while, do, three loop statement in Java _java

Source: Internet
Author: User

In this paper, we introduce the difference between the for-, while and do-three circular statements in Java, which are shown in the following detail:

The first type: For loop

The format of the loop structure for statement:

for (initialization expression; conditional expression; Operation expression after loop) {
loop body;
}

eg

Class dome_for2{public
static void Main (string[] args) {
//system.out.println ("Hello world!");
Find 1-10 even and
int sum = 0;
for (int i = 1;i<=10 i++) {
if (i%2 ==0) {//Judgment statement
sum +=i;//Sum
}
System.out.println (sum) ;
}
}

Output structure is 30

The second while statement

The format of the while statement of the loop structure:

Initialization statement;

while (judging conditional statement) {
circular body statement;
Control conditional statement;
}

eg

Class Demo_while {public
static void Main (string[] args) {
//seek 1-100 and
int sum = 0;//define initial and 0
int i = 1; /define the first number of sums to start while
(i <= 100) {//Judge conditional statement
sum = i;//sum = sum + i;
i++; Let the variable i self increase
}
System.out.println ("sum =" + sum);
}

The output is: sum = 5050

The third kind of do....while statement

Loop structure do...while statement format:

Initialization statement;

Do {
loop body statement;
Control conditional statement;
}while (judging conditional statement);

eg

Class Demo1_dowhile {public
static void Main (string[] args) {
//1-100 and
int sum = 0;//define variable sum to store the sum value C22/>int i = 1; The definition of
the variable I do {//does is
the dry//system.out.println ("i =" + i); Loop body statement
sum +=i;
i++;
}
while (i <= 100); Judge Conditional statement
System.out.println ("sum =" +sum); Output Results
}

Output result: Sum = 5050

Summary: The difference between three kinds of circular statements:

The 1.do...while Loop performs at least one loop body at a time.

2. The For,while cycle must first determine whether the condition is valid, and then decide whether to execute the Loop body statement.

The above is a small set of Java for you to introduce, while, do while three kinds of circular statements of the difference, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.