Advertisement



< Prev
Next >



While Loop





Java provides us an important looping structure using which we could execute one or multiple statements within a loop until a condition is met. The condition of while loop is tested for the boolean value - true or false.






while loop example


// while loop example

public class WhileEx1  
{

public static void main(String... ar) //main method starts
{

int i=0;

while(i<5)
{
	System.out.println("i =" + i);
	i++;
}

}   //main method ends

}   //class ends


Output


i = 0
i = 1
i = 2
i = 3
i = 4



Advertisement




While loop must have an iteration part in its body or it runs an endless loop


class WhileEx2 //class starts
{

public static void main(String... ar) //main method starts
{

int i=0;

while(i<5)
{
	System.out.println("i =" + i);
}



}   //main method ends

}   //class ends


Output


This program when executed, runs indefinitely in a loop because of the absence of incremental part to increment the value of i.



Please share this article -





< Prev
Next >
< Switch Statement
Do-While Loop>



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement