logo
Java While and Do-While
while Loop:
1. The while is an entry-controlled loop statement. The test condition is evaluated and if the condition is true, then the body of the loop is executed. 
 
2. The execution process is repeated until the test condition becomes false and the control is transferred out of the loop. 
 
3. On exit, the program continues with the statement immediately after the body of the loop. 
 
4.The body of the loop may have one or more statements. 
 
5. The braces are needed only if the body contains two or more statements. It's a good practice to use braces even if the body has only one statement

Syntax: 

Initialization Expression;
while ( Test Condition)
{
Body of the loop
Updation Expression
}
Java Images
while program
class Test 
{ 
public static void main(String[] args) 
{ 
int i=0;
while (i<10) 
{ 
System.out.println("freetmielearn"); 
i++; 
} 
} 
}
Output :
10 time's freetmielearn
while infinite loop
class Test 
{ 
public static void main(String[] args) 
{ 
int i=0; 
while (true) 
{ 
System.out.println("freetmielearn"); 
i++; 
}
} 
}
Output :
infinite loop
Do while
1) If we want to execute the loop body at least one time them we should go for do-while statement. 

2) In the do-while first body will be executed then only condition will be checked. 

3) In the do-while the while must be ends with semicolon otherwise we are getting compilation error. 

4) do is taking the body and while is taking the condition and the condition must be Boolean condition.

Initialization Expression;
do {
statement(s)
updation expression;
} while(test condition);
Java Images
do while program
class Test 
{ 
public static void main(String[] args) 
{ 
int i=0; 
do 
{ 
System.out.println("freetmielearn"); 
i++; 
}while (i<10); 
} 
}
Output :
10's times freetmielearn
do while infinite loop
class Test 
{ 
public static void main(String[] args) 
{ 
int i=0; 
do 
{ 
System.out.println("freetimelearn");
} 
while (true); 
System.out.println("hi;//unreachable statement 
} 
}
Output :
infinite loop