banner
 

While Loop

       
    It is often the case in programming that you want to do something a fixed number of times. Perhaps you want to calculate gross salaries of ten different persons, or you want to convert temperatures from centigrade to fahrenheit for 15 different cities.

initialise loop counter ;
while ( test loop counter using a condition )
{
 do this ;
 and this ;
 increment loop counter ;
}

example:-

int i=0
while ( i<5)
{
System.out.println("Hello");
i++;
}
     Here the out put of this example will be
Hello
Hello
Hello
Hello
Hello

Nesting of While Loop


int i=0,j;
while ( i<3)
{
j=0
while(j<2)
{
System.out.println("Hello");
j++;
}
i++;
System.out.println("");
}
     Here the out put of this example will be
Hello          Hello
Hello          Hello
Hello          Hello