Java language----The difference introduction of three kinds of circular statements _java

Source: Internet
Author: User

The first type: For loop

The format of the loop structure for statement:

for (initialization expression; conditional expression; loop action expression) {
Circulation 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 (judgment condition statement) {
Circular body statement;
Control conditional statement;
}

eg

Class Demo_while {public
  static void Main (string[] args) {
    //seek 1-100 and
    int sum = 0;          Defines the initial and 0
    int i = 1;            Defines the first number of sums to start while
    (i <=) {        //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 {
Circular body statement;
Control conditional statement;
}while (judging conditional statement);

eg

Class Demo1_dowhile {public
  static void Main (string[] args) {
    //seek 1-100 and
    int sum = 0;                Defines the variable sum, which is used to store the sum of the value
    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 <=);              Judge Conditional statement
    System.out.println ("sum =" +sum);      Output results
    }
  }

Output result: Sum = 5050

Summary: The difference between three kinds of circular statements:

1. The Do...while loop executes at least once for the loop body.

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

The above Java language----Three kinds of circular statements of the difference is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.