Java day-05

Source: Internet
Author: User

Loop structure: Whiledowhile
While syntax format:
while (conditional expression)
{
Execute the statement;
}

        int x = 1;          while (X<3) {          System.out.println ("x=" +x);          X++        ;        } int a = 1;

Exercise: sum=1+100=5050

        int i=1,sum=0;          while (i<=100)        {            sum=sum+i;            I+ +;            System.out.println ("sum=" +sum);//output statement written in loop body        }

The process of outputting each overlay

        int i=1,sum=0;          while (i<=100)        {            sum=sum+i;            I+ +;        }        System.out.println ("sum=" +sum); // outside the loop body

Operation Result:

 Package cn.itcast.test;  Public class While_demo {
the number of times that can be divisible by 6 within//100
     Public Static void Main (string[] args) {            int i=1;         int count=0;          while (i<=100)        {            if(i%6==0)            count+ +;            I+ +;        }        System.out.println ("conut=" +count);}    }

Operation Result:

Do and syntax format:

Do
{
Execute the statement;
}while (conditional expression);

 do   {System.out.println ( "a=            "+a);        A  ++;  while  (a<3); 
 Package cn.itcast.test;  Public class Dowhile_demo {    publicstaticvoid  main (string[] args) {          int i=1,sum=0;          Do         {            sum=sum+i;            I+ +;        }  while (i<=100);//semicolon remember not to forget        System.out.println ("sum=" +sum);}    }

Java day-05

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.