You are on page 1of 15

M.

Sc 1st year

Computational Techniques
Lecture 1.6
Loops control construct:

While loop
Loop runs infinite times until some condition
is not satisfied

Iterative loops or counting loop


Loop runs for finite number of times which
we know already
While Loop:
Code for while loop looks like following:

Code executed until logical expression


become true and then it is exit
Do while loop:

Here if logical expression is true then the


statement are executed and if false then
terminated
Iterative or counting loops: which executes
for finite number of times
In above, index is some integer variable
used as loop counter or loop index

 istart, iend and incr are variable which


control value of index
Incr known as index increment is optional. If
it is not specified then its value is taken as 1.

The index loop and control parameter should


be of integer type. Real type is declared
obsolete in f90 and omitted completely in f95
Example of Do Loop: Find the factorial

Fortran Code:

Above code will find the factorial of n.


Number of iterations:

Examples:
Following code will be executed 10 times
Following code will not be executed as
Following code will be executed four times
It is possible to branch out of do loop while
loop is executing
Cycle statement:
Example:

Result:
Exit statement:
Example:

Result:
Nested Do Loop:

Example:

Results:
Note that while using the nested Do loop we should name
the Do loops. Otherwise if by mistake, some End Do is
deleted then it becomes difficult to know that whose
End Do is missing.

You might also like