While Loop In Java

The while loop is used to iterate the part of the program several times and if the iteration is not fixed then you can use while loop.

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

If you pass true in the while loop then it will be the infinitive loop(endless loop).

class InfiniteLoop
{
public static void main(String args[])
{
while(true)
{
System.out.println("Infinite loop");
}
}
}

Output:
Infinite loop
Infinite loop
Infinite loop
Infinite loop
-------------
ctrl+c - to stop the loop

 

Leave a Reply