Professional Documents
Culture Documents
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