0% found this document useful (0 votes)
67 views4 pages

08 06 2015iteration Through Loops

This document discusses loops in Java, including the three types of loops (for, while, do-while), the elements that control loops (initialization, test, and update expressions), and nested and infinite loops. It provides examples of using loops with different expressions omitted and examples of break and continue statements.

Uploaded by

Sagargn Sagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views4 pages

08 06 2015iteration Through Loops

This document discusses loops in Java, including the three types of loops (for, while, do-while), the elements that control loops (initialization, test, and update expressions), and nested and infinite loops. It provides examples of using loops with different expressions omitted and examples of break and continue statements.

Uploaded by

Sagargn Sagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Iteration 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.

Different ways of using for loop


The expressions in for loop is optional. The following are the loop fragments omitting expressions:
int i=10;
for( ; i<=50; i+=10)
System.out.print(i+ \t);

Initialization expression is written before the loop


block and omitted in the for statement.
Output : 10 20 30 40 50

int i=10;
for( ; ; i+=10)
System.out.print(i+ \t);

Initialization and test expressions are omitted in


the for statement. This creates an infinite loop as
does test for any condition.
Output : 10 20 30 40 50 60 70 ..
Initialization and update expressions are omitted
in the for statement. This creates an infinite loop
as the condition i<=10 is always true.
Output : 10 10 10 10 10 10 10 ..
All the three expressions are omitted in the for
statement and Inititalization expression is written
before the loop. This also creates an infinite loop
as there is no expression to check the condition to
stop the loop or an update expression to update
the value of the variable.
Output : 10 10 10 10 10 10 10 ..
All the three expressions are omitted in the for
statement. This also creates an infinite loop as
there is no expression to check the condition to
stop the loop or an update expression to update
the value of the variable.
Output : 10 10 10 10 10 10 10 ..

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.

Null loop or body less loop or time delay loop


A loop which does not include any statement in its body is called a null/body less loop. The purpose is to
simply to create a delay during execution.
Eg :
int i;
for( i=1; i<=100 ; i++) ;

or

for( i=1; i<=100 ; i++)


{
;
}

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

You might also like