08 06 2015iteration Through Loops
08 06 2015iteration Through Loops
A loop is a set of statements that get executed repeatedly until a certain condition is fulfilled. The Java
provides three kinds of loops/iteration statements : for loop, while loop & do.while loop.
Elements that control a loop (Parts of a loop)
1. Initialization Expression(s): Before entering in the loop, its control variable must be initialized.
The initialization of the control variable takes place under the Initialization Expression. This gives
the loop variable its first value. The initialization expression is executed only once in the
beginning of the loop.
2. Text Expression: The text expression is an expression whose truth value decides whether the
loop-body will be executed or not. If the test expression evaluates to true i.e. 1, the loop body
gets executed, otherwise the loop is terminated.
3. Update Expression: The update expressions change the value of the loop variable(s). The update
expression is executed at the end of the loop after the loop-body is executed.
4. The Body0of-the-loop: The statements that are executed repeatedly (as long as the testexpression in not zero) form the body of the loop. In an entry-controlled loop, first the test
expression is evaluated and if it is true, the body of the loop is executed; if the test expression
evaluates to false, the loop is terminated. In an exit controlled loop, the body of the loop is
executed and then the test expression is evaluated. If it evaluates to false, the loop is
terminated otherwise repeated.
Java provides three kinds of loops : for loop, while loop and do-while loop.
All the three loop constructs of Java repeat a set of statements as long as a specified condition remains
true. This condition is generally referred to as a loop control and a true condition is the one that returns
Boolean true value and the false condition is the one that returns Boolean false value.
For Loop:
The for loop is used for fixed number of iterations. All its loop-control elements are gathered in one
place. It is an entry controlled loop. The general form/syntax of the for loop:
for( initialization expression; test expression; update expression)
{
body of the loop
}
while Loop:
The while loop is used when number of iterations(repetitions) is not known before hand. It is an entry
controlled loop. The syntax of while loop is:
Initialization expression
while(test expression)
{
Loop body
Update expression
}
do-while loop
It is also used for unknown number of iterations. It is an example of exit controlled loop. The syntax of
while loop is:
Initialization expression
do
{
Loop body
Update expression
} while(test expression);
Nested Loop:
A loop within another loop is called a nested loop. The following is an example of nested loop.
for (int i=1;i<=5; i++)
// outer loop
{
Output:
*
for(j=1;j<=I; j++)
//inner loop
* *
{
* * *
System.out.print( * );
* * * *
}
* * * * *
System.out.println( );
}
The inner for loop is executed for each value of i. The variable i takes values 1,2,3,4 and 5. The inner
loop is executed once for i=1 according to condition j<=I, twice for i=2 , so on.
int i=10;
for( ; ; i+=10)
System.out.print(i+ \t);
int i=10;
for( ; i<=50 ; )
System.out.print(i+ \t);
int i=10;
for( ; ; )
System.out.print(i+ \t);
for( ; ; )
System.out.print(10+ \t);
Note :
1. In for loop, do use semicolon( ; ) if any expression is omitted.
2. Infinite loops can also be created in while and do.while loops omitting the update expression.
Infinite loop or Endless loop:
An infinite loop is a loop which never ends. If an update expression is omitted in a loop then the
condition is always satisfied i.e. evaluates to true. In such cases the loop becomes an infinite loop.
Eg : Refer to the above table.
or
Jump statements :
(i)
break :
A break statement in a loop is used to terminate (immediate exit) the loop.
When a break statement is encountered the control exits the loop block and executes the
statement following the loop.
(ii)
continue :
A continue statement in a loop is used to skip the execution of the statements in the current
iteration and resume with the next iteration.
When a continue statement is encountered the control skips the execution of the
statements in the current iteration and continues with the next iteration.
break STATEMENT
for(int i=1; i<=20 ; i++ )
{
if( i % 5==0)
break;
System.out.print(i + \t);
}
continueSTATEMENT
for(int i=1; i<=20 ; i++ )
{
if( i % 5==0)
continue;
}
System.out.print(i + \t);
Output :
1 2 3 4
Output :
1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19