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