The difference between three types of cyclic structures in Java

Source: Internet
Author: User

First type: For loop

Loop structure for statement format:for (initialize an expression; a conditional expression; an action expression after a loop) {circulation body; }eg:
1 class dome_for2{2 public     static void Main (string[] args) {3         //system.out.println ("Hello world!"); 4         //Request 1- 10 even and 5         int sum = 0; 6 for         (int i = 1;i<=10; i++) {7             if (i%2 ==0) {            //Judgment statement 8                 sum +=i;
   
    //summation 9             }10         }11         System.out.println (sum);     }13}
   

The output structure is 30

Second while statement

The format of the loop structure while statement:

  initialization statements;

While (judging condition statement) {loop body statement;control condition statement;}eg
1 class Demo_while {2 public     static void Main (string[] args) {3         //Seek 1-100 and 4         int sum = 0;                    Defines the initial and is 0 5         int i = 1;                        Defines the first number of start sums 6         while (i <=) {                //Judgment condition Statement 7             sum + = i;                    sum = sum + i; 8             i++;                        Let the variable i increment by 9         }10         System.out.println ("sum =" + sum);     }12}

The output is: sum = 5050

The third type of Do....while statement

The format of the loop structure Do...while statement:

initialization statements;Do {loop body statement;control condition statement;}while (Judgment condition statement);eg:
1 class Demo1_dowhile {2 public     static void Main (string[] args) {3         //Seeking 1-100 and 4         int sum = 0;                                Defines the variable sum, which is used to store the sum value 5         int i = 1;                                    Define the variable i 6                                         do {//does  is dry 7             //system.out.println ("i =" + i);             Loop Body Statement 8             sum +=i; 9             i++;10         }11 while         (i <=);                            Judge Conditional statement         System.out.println ("sum =" +sum);            Output results         }14     }

Output result: Sum = 5050

Summarize:  The difference between the three circular statements:

The 1.do...while loop executes at least one loop body.
2. The For,while cycle must first determine whether the condition is true, and then decide whether to execute the Loop body statement.

The difference between three types of cyclic structures in Java

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.