Iteration
in Java
An iteration statement can result in
a statement (or set of statements in a compound statement) being executed
never, once or many times. What determines whether the statement(s) will
be executed or not is the result of a test that is applied
either before or after an execution of the loop statements. If the result
of the test is true then loop will continue, if the result is false
the loop will exit and the first statement following the end of the loop
will be the next to be executed..
There are three forms of iteration
statement implemented in Java:
In this module we shall concentrate on
the first two of these iteration statements. They are all technically equivalent;
the choice as to which form of iteration statement to implement in a program
is made by each programmer on the grounds of style or convenience or expressive
power. (The expressive power of a programming language statement is
how clearly and effectively it communicates to a reader the effect the
programmer wants to achieve).
Iteration:
WHILE
A while
statement works in a very similar
way to an if
statement, except that if the test
is true, and the statements executed, the test is repeated, and the statements
repeated until the test is false. Using pseudocode code we can illustrate
how a while
statement works. Consider the following
pseudocode for a system to process bank transactions
while
( more transactions to process for account 00710 )
process
the next transaction for account 00710
This illustrates the general form of an
while
statement, i.e.:
while
(test)
action
to perform if test is true
To implement our fire alarm pseudocode
as a valid Java statement we could write the following:
while
( moreTransactions( 00710) )
processNextTransaction(
00710 );
(of course we are assuming the class has
methods moreTransactions()
and
processNextTransaction()).
The way a while
statement can cause deviation from
the default sequential flow of execution can be illustrated as follows.
Consider this pseudocode design:
First statementA
is executed. Then the test is made.
Let us assume that the method moreTransaction(00710)returns
false. Then the loop statement is skipped, and the next statement to be
executed is statementC:
However, consider the situation
where after statementA
has been executed, the test (i.e.
method moreTransaction(00710)
) returns true the
first time. Then
statementB is executed.
The test is conducted again. Let us assume that this time method
moreTransaction(00710)returns
false. Then the loop statement is then skipped, and the next statement
to be executed is statementC:
statementA
statementB
statementC
Now consider the situation where after
statementA
has been executed, the test (i.e.
method moreTransaction(00710)
) first returns true. Then
statementB is executed.
The test is conducted again. Let us assume that
moreTransaction(00710)returns
true again (i.e. there were 2 transactions for this account waiting to
be processed). Then
statementB is executed
a second time. The test is conducted again. Let us assume that this time
method moreTransaction(00710)returns
false Then the loop statement is then skipped, and the next statement to
be executed is statementC:
statementA
statementB
statementB
statementC
Thus the result of an iteration statement
means that some statements may not be executed, or may be executed
once, or be executed many times.
Back
to top
RITSEC - Global Campus
Copyright © 1999 RITSEC- Middlesex
University. All rights reserved.
webmaster@globalcampus.com.eg
|