You are on page 1of 11

Fortran Programming

Looping Structure
Introduction
Weve seen selection structures (IF statements,
SELECT CASE statements) which are Think
Before You Act kind of statements.
We will now look at statements which performs
Repetitive Tasks using looping structures
Looping structures allow some blocks of
structures to be repeated in a controllers manner
rather than reproducing the blocks multiple
times.
Consider the following:
PRINT*,Please Enter Your Name:
READ*,Name
PRINT*,Hello,Name
The above segment prompts the users to enter
his/her name. If there are 3 users who have to
enter their names, we can type the same
segment 3 times in our program. What if there
are 1000 users???
A looping structure can be used for this program
To perform repetitive actions in Fortran
programs
DO WHILE Loops
Consider this example:
PRINT *,Please Enter The Number of Users:
READ*,n
Counter = 1
DO WHILE (Counter<=n)
PRINT*,Please Enter Your Name:
READ*,Name
PRINT*,Thank you. Next User Please.
Counter=Counter+1 ! Counter counts up to n
END DO
DO WHILE (condition)
block of statements
END DO
The DO WHILE loop continues iterating and
doing block-of-statements as long as
condition is true
condition accepts relational operators
(==,/=,<,>,>=,<=) and logical operators
(.NOT.,.AND.,etc.) together with operands form
logical expressions.
Looping continues until condition becomes false.
The entity inside the loop has to change the
value of the condition. Otherwise the loop will
continue indefinitely.
It is common practice to design a DO WHILE
loop that allows the program to break out from
any infinite loop:
- e.g. Strategically place a PRINT statement
inside the loop that display the values involved
in the condition so that the value can be
monitored as it become unstable.
DO WHILE structure is also known as pre-tested
loop. This means that the loops condition is
tested first when the program flow encounters
the loop, before entering the loop.
DO WHILE (condition) DO WHILE (condition)

block block

END DO END DO

(condition TRUE) (condition FALSE)

This is like the IF statement too (Explain)


The Counter A useful tool that often
used in computer programs to count the
number of events that has occurred.
So, how does the incremental statement
work?
Counter_z = 0 Answer = The counter is set
(initialised) with a value of

Counter_z = Counter_z+1 zero (0)


Answer = The counter now holds
(contains) a value of 1
Counter_z = Counter_z+1
Answer = The counter now holds
a value of 2
Counter_z = Counter_z+1
Answer = The counter now holds
a value of 3
Whatever is the previous value of
Counter_z, the value is added (increased)
by one (1)
Another way of looking at this is shown
below:
Counter_znew = Counter_zold + 1
Note that 1 in the above statement need
not be just 1 but can be a variable t, for
example, where t can be any integer.
Example
For when an event occurs, the value of
Counter_y increases by 3. This is done
using a DO WHILE loop. For every time
this loop is repeated, another counter will
increase its value (by 1). This allows a
record to be kept on the amount of times
the event has occurred that causes
Counter_y to change. If we do not want
this loop to proceed infinite amount of
time, we place a limit number of loops
say 15.
Counter_y = 0
t=3
n = 15
LoopCounter = 1

DO WHILE (LoopCounter <= n)


Counter_y = Counter_y + t
LoopCounter = LoopCounter + 1
END DO

You might also like