while loop


We use while loop in Java Programming to certain number of iterations. And it will not work like for loop. Let’s check it in listing 3 for details, but in the listing 3 we use the same program which we did in listing 2. But we will change for loop in while.
Listing 3: Use of While Loop in Java Programming
public class WhileLoop{
public static void main(String args[])
{
//printing in ascending order
//from 1 to 10
//using increment operator
int i = 1;
while(i <= 10)
{
System.out.print(i+" ");
i++;
}
System.out.println();
//printing in descending order
//from 10 to 1
//using decrement operator
i = 10;
while (i >= 1)
{
System.out.print(i+" ");
i--;
}
}
}

Comments