Loop control Statements

Source: Internet
Author: User

 1. There are 3 types of loop control statements in Java, namely While,do ... while and for loops.
2. While loop in the form:
while (boolean-expression)
{
Code to be executed
}

  

  Class   whiletest{ public  static  void   main (string[] args) {int  a = 2; //         loop variable  int  sum = 0; //                 while             (A <= 100 += A;        A  + = 2;    } System.out.println (sum); }}

3. Do...while cycle, the new type is:
Do
{
Code to be executed
}
while (Boolean expression);

 Public classdowhiletest{ Public Static voidMain (string[] args) {intA = 2;//Loop Variable        intsum = 0;//results after the storage is added         Do{sum+=A; A+ = 2; }         while(A <= 100);        SYSTEM.OUT.PRINTLN (sum); intb = 2;  while(b < 1) {System.out.println ("Hello World"); }         Do{System.out.println ("Welcome"); }         while(b < 1); }}

  4. The difference between while and Do...while: if the first judgement of a Boolean expression is false, then the while loop does not execute once, and the Do...while loop executes once.

If the Boolean expression evaluates to True for the first time, then the while loop is equivalent to the Do...while loop.
5. For loop (one of the most used loops), in the form:
for (variable initialization; condition judgment; stepping)
{
Code to be executed
}
The execution process for the FOR loop:
1) perform variable initialization.
2) perform the condition judgment. If the condition determines that the result is false, exit the for loop, start executing the code after the loop, or, if the condition is true, execute the code inside the For loop.
3) perform stepping.
4) Repeat step 2.

 Public classfortest{ Public Static voidMain (string[] args) {intsum = 0;//results after the storage is added                 for(inti = 2; I <= 100; i + = 2) {sum+=i;        } System.out.println (sum); intsum2 = 0;//results after the storage is added         for(inti = 100; I >= 1; i--) {sum2+=i;    } System.out.println (SUM2); }}

Loop control Statements

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.