Java Learning Notes (iv)-Looping statements

Source: Internet
Author: User

While loop

/* * While loop * loop variable, you can control the number of cycles. */ public  class  test{public  static  void  main  (string[] args) {int  count = 5 ; while         (Count > 0 ) {System.        out . println ( "Hello World"  + count--); } System.        out . println (count); System.    out . println (); }}
/* * While loop * Implement 1~100 's sum */ public  class  test{ Public  static  void  main     (string[] args) {//i represents a loop variable, at which I can not only control the number of cycles, I can also represent a specific value.         int  i = 1 ; int  sum = 0 ;        //for storing results  while         (I <= 100 ) {sum + = i;            //sum = sum + i;  i++;        //loop variable self-increment } System.    out . println (sum); }}

Do.. While loop

/ * * do. While loop * While loop difference? */ Public classtest{ Public Static void Main(string[] args) {//cyclic variable, mainly used for control loops.         intCount1 =0; Do{System. out. println ("Hello World"); } while(Count1 >0);//do. While loop to achieve the sum of 1~100        inti =1;intsum =0; Do{sum + = i;//sum = sum + i;i++; } while(I <= -); System. out. println (sum); }}

For loop

/ * * For loop */ Public classtest{ Public Static void Main(string[] args) { for(inti =0; I <5; i++) {System. out. println ("Hello World"); }/* * For statement implementation of 1~100 */        intsum =0; for(inti =1; I <= -;        i++) {sum + = i; } System. out. println (sum);//int i = 0;//For (SYSTEM.OUT.PRINTLN ("Expression 1"); i++ < 2; System.out.println ("Expression 3"))//      {//System.out.println ("loop Body");//      }}}
/ * * For attention points * / Public classtest{ Public Static void Main(string[] args) {//Expression 3 can have multiple statements         for(intx =0, y =Ten; X <5; x + +, y++) {}//For three expressions are optional        inti =1;intsum =0; for(; I <= -;)            {sum + = i;        i++; } System. out. println (sum);//second expression if not, indicates permanent true         for(;;) {System. out. println ("Hello World"); }    }}

Title: Print output 1~100 between the even and

/* Title: Print output 1~100 even between and */ Public classtest{ Public Static void Main(string[] args) {//while        intI1 =1;intSUM1 =0; while(I1 <= -)        {if(I1%2==0) {sum1 + = I1;        } i1++; } System. out. println (SUM1);//for        intsum2 =0; for(intI2 =1; I2 <= -; i2++) {if(I2%2==0) {sum2 + = i2; }} System. out. println (SUM2); }}

Title: All leap years in print output 1000-2050

/* Title: All leap years in print output 1000-2050 */ Public classtest{ Public Static void Main(string[] args) {//while        intYear = +; while(Year <=2050)        {if(Year%4==0&& Year% -!=0) || (Year% -==0)) {System. out. println (year);        } year++; }//for         for(inty = +; Y <=2050; y++) {if(Year%4==0&& Year% -!=0) || (Year% -==0)) {System. out. println (year); }        }    }}

Break statement

/ * * Break * in the loop, once the break statement is run, the loop is terminated. */ Public classtest{ Public Static void Main(string[] args) {//break         for(inti = -; i >0; i--) {ifI7==0) {System. out. println (i); Break; }        }intCount =Ten; while(true)        {if(Count%7==0)            { Break; } System. out. println (count--); } System. out. println ("for external code"); }}

Continue statements

/* * Continue * terminates this cycle and enters the next cycle. */ Public classtest{ Public Static void Main(string[] args) {inti =0; while(I++ <5)        {ifI3==0)            {Continue; } System. out. println (i); }//Through continue, realize the sum of even numbers between 1~100        intsum =0;intA =1; while(A <= -)        {ifA2!=0) {a++;Continue;            } sum + = A;        a++; } System. out. println (sum); }}

Nested loops

/ * Nested loops * / Public classtest{ Public Static void Main(string[] args) { for(Charc =' A '; C <=' D '; C + +) { for(inti =0; I <=3; i++) {System. out. print (C +""+ i +"\ T"); } System. out. println ();//Line break}    }}

Required, use * to print out a right triangle

/* * required, use * print out a right triangle * */ Public classtest{/*     *     **     ***     ****     *****     ******     *******     */     Public Static void Main(string[] args) {//print seven lines         for(introw =0; Row <7; row++) { for(intCol =0; Col <= Row; col++) {System. out. Print ("*"); } System. out. println (); }    }}

Print isosceles triangle

/* 5-row * 2*row-1 * * * * * * * * **************** * * Public classtest{ Public Static void Main(string[] args) { for(introw =1; Row <=5; row++) {//Print space             for(intSpa =1; Spa <=5-row; spa++) {System. out. Print (" "); }//print *             for(intCol =1; Col <= (2* Row-1); col++) {System. out. Print ("*"); } System. out. println (); }    }}

Java Learning Notes (iv)-Looping 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.