Sie sind auf Seite 1von 14

In Fortran

By Dr. Agus Dwi Anggono


Logical Expressions
A logical expression consists of one or more logical
operators and logical, numeric, or relational operands.
For example, the following logical expression is valid
A+B/(A-1) .AND. .NOT. D+B/(D-1)

Expressions and Assignment Statements


A*B+C*ABC == X*Y+DM/ZZ .AND. .NOT. K*B > TT
Constant
The simplest form of an expression is a constant.
Here are some integer constants:
1
0
-100
32767
+15
Then we have real constants:
1.0
-0.25
2.0E6
3.333E-1
Read statement
An action statement.
READ ( io-control-spec-list ) [ input-item-list ]

read (*,*) a
read (*,*) a, b, c

read *, a
read *, a, b, c
Write statement
write (*,*) 'Area = ', area
write (*,*) A = ', a, B = , b

print *, 'Area = ', area


print *, A = ', a, B = , b
Program Structure
Examine the following short program:

program sum !a: name of program


!an example of program structure !b: a comment
real :: answer,x,y !c: declarations
print *, 'Enter two numbers' !d: output
read *, x !e: input
read *, y !e: input
answer=x+y !f: arithmetic
print *, 'The total is ', answer !g: output
end program sum !h: end of program
More on Input and Output
program io
real :: x,y,z
print *, 'enter the values x,y and z'
read *, x,y,z
print *, 'the values you typed are for z,y,x are: ',z,y,x
end program io
The following program has a
number of errors.
program bug
this program is full of errors
real :: a,b,c a = b + c
read *,c
print *,a
end program simple
Implicit none
Always use implicit none at the start of every program.
The old FORTRAN compilers used an implicit convention
that integers have names starting with the letters in the
range i n, all the others being real

program convert
implicit none
integer :: pounds,pence,total
print *,'What is your name?'
read *,name
print *, 'Hi ',name,
end program convert
Area of Plane Shapes
Subroutine or function subprogram
Re-using code the subroutine.
A Fortran subroutine is a block of code that performs
some operation on the input variables, and as a result
of calling the subroutine, the input variables are
modified.

! sub1 is a subroutine defined elsewhere ! sub1 performs


some operation on input variables e and f call sub1(e, f)
! now e or f, or both (or neither) may be modified
Subroutine
A subroutine can be used to return several values
through its arguments.
Algorithm and Flowchart
Start
Definition, N = 0; X = 0
Calulate X = X + N2
If x < 6, N = N+1
If x not < 6, X is accepted
End

Das könnte Ihnen auch gefallen