< 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


0
1
2
3
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 our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2