Related post

While and Do..while loop in Java

Java while loop


WHILE AND DO WHILE LOOP

In this tutorial, you would learn the use of while and do while loop in Java 



WHILE LOOP


The java while loop is used to run a code repeatedly until a certain condition is met. This is used basically to test the code within the parenthesis and once the condition is met, it gives result.



SYNTAX




while ( condition ) {

 //Code;

//Increment / decrement;

}

 


Explanation

 • The parenthesis will contain the condition of the code

 • Once the condition is met, the body of the loop will be executed.


Let's take an example in Java




//by Zhullyblog

public class sample{

 public static void main(String[] args) {

  int x = 1;

  while ( x <20 ){

    System.out.println("The value of x is : " + x );

    x++;

   }

  }

}

Screenshot

Java while loop

Result

Java while loop example



The code above is just a simple test that shows how Java while loop works.



DO....WHILE LOOP


The do...while loop is quite similar to while loop but in this case, the body of the loop is executed before the condition is tested.


SYNTAX



do {

  //Statement ;

 //increment /decrement;

} while ( condition );




Example



public class sample{

  public static void main(String args[]){

         int x =1;

         do{

              System.out.println(x);

              x++;

         }while(x<10);

  }

}



Screenshot

Java do while loop

Let's move


Continue statement in Java


Java Object Oriented Programming


Constructors in Java programming


Inheritance in Java



Please share 







Follow us on




Comments