< Prev
Next >



While Loop





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





While loop must have an iteration part in its body or it ends 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 -

Facebook Google Pinterest Reddit Tumblr Twitter




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

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page

Notifications



Please check our latest addition

PYTHON