JAVA while loop, Do-while Loop, for loop

Source: Internet
Author: User

One, while loop

Instance:

 Public class test{    publicstaticvoid  main (string[] args) {        int i    =    1;          while (i<30) {            System.out.println (i);            I+ +;     }}}

Second, Do-while cycle

 Public class test{    publicstaticvoid  main (string[] args) {        int i    =    1;          Do {            System.out.println (i);            I++        ;        }  while (i<30);         // Do-while Loop, first executed once, and then in judgment, if the condition is set, in the loop execution, if not set, continue down execution     }}

Do-while instance (guess number game):

ImportJava.util.*; Public classtest{ Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); Random RA=NewRandom (); intr = Ra.nextint (10); intJ; System.out.println ("======= Guess number game =======");  Do{System.out.println ("Please enter the number to guess (0-9):"); intnum =In.nextint (); if(num<R) {System.out.println ("Very small."); J= 1; }            Else if(num>R) {System.out.println ("Great!"); J= 2; }            Else{System.out.println ("Guess right."); J= 11; }        }         while(j!=11); }}

Third, for Loop

 Public class test{    publicstaticvoid  main (string[] args) {        for ( int i=1;i<30;i++) {            System.out.println (i);     }}}

JAVA while loop, Do-while Loop, for loop

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.