FORTRAN 90 L e c

advertisement
FORTRAN 90
Lecturer : Rafel Hekmat Hameed
University of Babylon
Subject : Fortran 90
College of Engineering
Year : Second B.Sc.
Mechanical Engineering Dep.
Repetitive Execution
C
Coouunnttiinngg D
DO
O--LLoooopp
The syntax of the counting loop is the following:
DO control-var = initial-value , final-value , [step-size]
statements
END DO
Where control-var is an INTEGER variable, initial-value and final-value
are two INTEGER expressions, and step-size is also an INTEGER expression
whose value cannot be zero. statements is a sequence of statements and is
usually referred to as the body of the DO-loop. You can use any executable
statement within a DO-loop, including IF-THEN-ELSE-END IF and even
another DO-loop.
DO i=1,10,1
WRITE (*,*) i
!write numbers 1 to 10
END DO
DO i= 20 , 1 , -7
PRINT *, i
ENDDO
!Prints 3 lines containing 20, 13 and 6
ϭ
DO i = 1 , 3
PRINT *, 7*i-3
ENDDO
!Prints 3 lines containing 4, 10 and 17
DO i = 3 , 1
PRINT *, 7*i-3
ENDDO
!Does nothing
EXAMPLE
The factorial of a non-negative integer n, written as n!, is defined as
follows:
Write a program that reads in an integer and computes its factorial. This program
should detect if the input is negative and display an error message.
PROGRAM Factorial
IMPLICIT NONE
INTEGER :: N, i, Answer
READ(*,*) N
IF (N < 0) THEN
! input error if N < 0
WRITE(*,*) 'ERROR: N must be non-negative'
WRITE(*,*) 'Your input N = ', N
ELSE IF (N == 0) THEN
! 0! = 1
WRITE(*,*) '0! = 1'
ELSE
! N > 0 here
Answer = 1
! initially N! = 1
DO i = 1, N
! for each i = 1, 2, ..., N
Answer = Answer * i ! multiply i to Answer
END DO
WRITE(*,*) N, '! = ', Answer
END IF
END
Ϯ
EXAMPLE
Write a Fortran program to find the number of odd and even integers
among (10) numbers; [read the numbers from text data file]:
20
Program number
Implicit none
integer:: odd,even,i
real::x
open(5,file='data.dat')
odd=0
even=0
do i=1,10
read(5,*)x
if (x==(int(x/2.0)*2)) then
even=even+1
else
odd=odd+1
endif
enddo
write(*,20) odd,even
format(2x,'number of odd=',i2,/,2x, 'number of even=',i2)
end
G
Geenneerraall D
DO
O--LLoooopp w
wiitthh EEX
XIITT
The general DO-loop has a form as follows:
DO
statements
END DO
EXAMPLE
REAL :: x, y, z
DO
READ(*,*) x
y = x*x
z = x*x*x
WRITE(*,*) x, ' square = ', y, ' cube = ', z
END DO
ϯ
This loop is executed over and over and has no chance to stop at all. A
loop that never stops is usually referred to as an infinite loop. To stop the
iteration of a DO-loop, we need something else.
TThhee EEX
XIITT SSttaatteem
meenntt
The EXIT is as simple as writing down the word EXIT. It is used to bail
out the containing loop.
DO
statements-1
EXIT
statements-2
END DO
After an EXIT statement has been executed control is passed to the first
statement after the loop. For example:
INTEGER :: value=0, total=0
...
DO
READ(*,*) value
!read in a number
IF (value==0) EXIT
!if nothing to add, exit loop
total = total + value
!calculate running total
END DO
EXAMPLE
Write a fortran 90 program to print the power of (3) i.e (30,31, 32, 33,…..) ,
stop the program if the value of power is greater than 2000.
Program power3
Implicit none
Integer :: power =1
Do
Write(*,10) power
ϰ
10
format (1x, i5)
Power=4*power
If (power > 2000) exit
Enddo
End
EXAMPLE
Write a Fortran program to show how to write a counting loop with real
numbers. Variable ‫ ܠ‬receives values −1.0, -0.75, -0.5, -0.0, 0.25, 0.5, 0.75, and
1.0; [without using do-loops]
Program counting_loop
Implicit none
real , parameter :: lower = -1.0
real , parameter :: upper = 1.0
real , parameter :: step = 0.25
real :: x=lower
do
if (x>upper) exit
write(*,50) x
50 format(3x,f5.2)
x=x+ step
enddo
end
ϱ
Download