Java while loop

 Java while loop

The while loop is also used to iterate over the number of statements multiple times. However, if we don't know the number of iterations in advance, it is recommended to use a while loop. Unlike for loop, the initialization and increment/decrement doesn't take place inside the loop statement in while loop.

It is also known as the entry-controlled loop since the condition is checked at the start of the loop. If the condition is true, then the loop body will be executed; otherwise, the statements after the loop will be executed.

The syntax of the while loop is given below.

  1. while(condition){    
  2. //looping statements   & incremental or decremental statement
  3. }    

The flow chart for the while loop is given in the following image.

Control Flow in Java

Consider the following example.

//print "I am Indian" 10 times.

  1. public class WhileLoop1 {
  2. public static void main(String []args){
  3. int i=1;
  4. while (i<=10){
  5. System.out.println("I am Indian");
  6. i++;
  7. }
  8. }
  9. }

Output:

I am Indian
I am Indian
I am Indian
I am Indian
I am Indian
I am Indian
I am Indian
I am Indian
I am Indian
I am Indian


Comments

Popular Topics

What is JSP. How to create one JSP application in Eclipse

How to develop a RESTful webservice in java ?

What is JDBC ?

Data Types in PL/SQL

What is Hibernate in java ?

If you have any query or suggestions, Please feel free to write us

Name

Email *

Message *