There are three iteration statements
1 while
2 do-while
3 for
All three create loops. Hence, they can be used where
some task is to be repeated number of times
Let us discuss all of them one by one
While
It continues execution until the expression on which it
depends evaluates to true. As soon as, value of
expression changes to false, loop will be terminated.
General syntax of while
while (someexpression)
{
// statements
}
Here, someexpression must evaluate to boolean value
As long as it is true statements inside block continue to get executed. When it changes to false, loop terminates and control passes to next line of code following the while block closing brace
do-while
It is similar in function to while block but with one important difference
General syntax of do-while
do
{
// statements
} while (someexpression)
The difference as you might have noticed is that when
the controlling expression is evaluated.
In while, it is evaluated before execution begins. Hence,
loop might not execute even once.
In do-while check is performed at the end.Hence, it is
guranteed that the loop would execute atleast once.
Examples
int i = 5 ;
while ( i < 5 )
{
System.out.println ( "Never printed" ) ;
}
Here, i < 5 results in false hence loop is not entered
do { System.out.println ( "Printed once" ) ; }
while ( i < 5 )
Here, loop is executed once as the expression i < 5 is checked after the loop body executes once.
To summarize, whenever you have a situation where loop body needs to be executed at least once, use do-while otherwise use while
for loop
It is a very powerful and flexible control block It has number of variations
General syntax of for
for ( initialization expression ; control expression ; update expression )
{
// statements to be executed
}
initialization expression : variables can be declared and initialized here
i = 5 // single variable
i = 5 , j = 6 // two variables seperated by comma
control expression : It must evaluate to a boolean. It's value determines whether the loop will execute and till when it will execute
update expression : variables forming the control expression can be updated here
Each of the above part is optional
Let us go through some examples to see the different variations
example 1
Standard form
for ( int i = 0 ; i < 5 ; i ++)
{
System.out.println ( "Value of i is : " + i ) ;
}
Steps in execution
a variable i is declared and initialized
b value of i is checked in control expression i < 5 . As it evaluates to true , loop body is executed printing Value of i is 0
c update expression is evaluated changing the value of i to 1
d Step b and c are repeated till update expression evaluates to false e when i = 5 , loop gets terminated
example 2
Initialization expression moved outside the loop
int i = 0 ;
for ( ; i < 5 ; i ++)
{
System.out.println ( "Value of i is : " + i ) ;
}
example 3
- Initialization expression moved outside the loop
- control expression moved inside the loop body
int i = 0 ;
for ( ; ; i ++)
{
- if ( i >= 5 )
{
break ;
} // if block ends
System.out.println ( "Value of i is : " + i ) ;
} // for loop ends
Here, the loop control logic is made with the help of if block and break statement.
As soon as value of i reaches 5 , if block is entered and break causes the loop
to terminate
example 4 - Initialization expression moved outside the loop
- control expression moved inside the loop body
- update expression moved inside the loop body
int i = 0 ;
for ( ; ; )
{
if ( i >= 5 )
{
break ;
} // if block ends
System.out.println ( "Value of i is : " + i ) ;
i ++ ;
} // for loop ends
As you can see, you have the option to keep as many expressions
inside the loop () and as many outside it.
Output is same in all the cases, only implementation way has changed
Using comma operator in for loop
Sometimes for loop depends on more then one variable. We can
initialize and update multiple variables seperated by comma as shown
int i , j ;
for ( i = 3 , j = 5 ; i < 10 ; i ++ , j ++)
{
// loop body
}
Note: Control expression must be a single expression
Nested loops
Java allows you to nest one loop inside the other. You will occassionaly
need nested loops to do some tasks
Following example shows the use of nested for loop's
// Print tables from 1 to 10 upto multiple of 10
for (int i = 1 ; i < 11 ; i ++)
{
System.out.println ("Table of " + i + "\n") ;
for (int j = 1 ; j < 11 ; j ++ )
{
System.out.println (i + " x " + j + " = " + (i*j) ) ;
} // inner for loop ends
System.out.println ("\n \n") ;
System.out.println ("------------------------------------------") ;
System.out.println ("\n \n") ;
} // outer for loop ends
For each iteration of the outer loop, inner loop executes 10 times.
After it's termination, statements following it are executed and then
the outer loop executes again till the control condition is true
Any of the loop can be nested inside another