Monday, April 15, 2013

While loop in Java

While loop in Java

In while loop
•       Initialization is done
•       Then condition is check
•         If condition is true then body of loop is executed
Format for while loop
While (condition)
{
Body of loop
}

In while loop condition is check first
If it is true then body of loop is executed
After this execution condition is again check
Body of loop is executed till the condition in
While loop is false
Example: Write the program to print the number of table

   In above program
class Table
{
public static void main(String[] args)
{
int num=4,table=0,j;
j=1; // initialize the value of j
while(j<11) // condition check
{
table=num*j;                 // body of loop
System.out.println(+table);
j++;
}
}
}  
We initialize the value of variable j
j=1
then condition in while loop is check
while(j<11)
if condition is true then body of loop is executed
if condition is false
then execution of program goes out side the loop
Output    
     

No comments:

Post a Comment