Java


Java Tutorial


Admission Enquiry Form

  


While Loop

What is while loop in Java?

While loop in Java is an Unknown loop in Java.It is used when the number of iteration are not known.

Syntax of while loop

while(condition)
{
//number of statements
}




Example of while loop:

// Java program of while loop.
public class WhileLoop {
public static void main(String[] args) {

int i;
i=1;
while(i<=10)
{
System.out.println("i is = "+i);
i++;
}//end of while loop

}//end of main method

}//end of class


Output:

i is = 1
i is = 2
i is = 3
i is = 4
i is = 5
i is = 6
i is = 7
i is = 8
i is = 9
i is = 10

In the above example, the while loop will execute 10 times because of condition given, in while loop we have to increment the value of variable inside the block of while loop