Lesson 4 - Learning
4.1 More on data types
4.2 Arithmetic operations
4.3 Concept of a computer variable
4.4 Declaration statements
4.5 Implicit declarations in FORTRAN
4.6 Integer division
4.7 Mixed Mode aritmetic
4.8 FORTRAN 90 - PRINT statement
4.9 Design and setup of a complete program
... 3 2 1 0 1 2 3 ...
/ integer divide : 17/5 = 3 (remainder of 2 thrown away)
to power = exponentiation
.16 0 00014 -0.3 6.02E23 (means 6.02 x l0exp23)
&nbs\ p; + add
&nbs\ p; - subtract
&nbs\ p; * multiply
/ integer divide : 17/5 = 3.4
** &bsp; raise
to power = exponentiation
'HELLO, WORLD' 'PROF. G. RATZER' '398-7082'
"HOW"S IT GOIN", EH?" '
4. LOGICAL DATA
.AND. logical AND -- result true if both are true
.OR. logical OR -- result true if at least one is true
.NOT. changes .TRUE. to .FALSE. and vice versa
FORTRAN ALSO ALLOWS
INTEGER (LEN=2) => 2 BYTES
REAL (LEN=4) => 4 BYTES
REAL (LEN=8) => 8 BYTES
DOUBLE PRECISION 8 BYTES
COMPLEX 8 BYTES
1- A quantity that can vary.
2- A location in memory.
3- A name used to refer to.
RULES FOR CHOOSING NAME
A) 1 to 6 characters ( FORTRAN 77 )
1 to 31 characters ( FORTRAN 90 )
B) First character must be A
... Z, rest A...Z plus 0...9
APOLLO13 -> NOT VALID IN FORTRAN 77
THISISTOOLONGTOBEAGOODVARIABLENAME -> NOT VALID
7UP -> NOT VALID
!ALLO -> NOT VALID
22CATCH -> NOT VALID
For each data type and corresponding
constant there are what are called declaration statements, which
are used to define the type of data to be associated with
a chosen FORTRAN variable. Thus
:: K, PTR
:: SUM, COST, VALUE
(LEN=20) :: NAME, ADDRSS
The declaration statements should
be placed at the beginning of a program, before any executable
statements like a PRINT statement. The above declarations define
K and PTR to be INTEGER variables and any data assigned to these
variables will be stored as a whole number, without any fractional
Likewise the variables SUM, COST
and VALUE should be used in calculations which may require a numeric
value with a fractional part.
The CHARACTER statement usually
has a length attribute - namely 20. This specifies that both the
variables NAME and ADDRSS may be used to store a string of up
to 20 alphanumeric characters.
The LOGICAL variable SINGLE may only be assigned the value .TRUE. or .FALSE.
You should ALWAYS declare all
the variables you will use in your program.
If you forget, FORTRAN will assume
variable names starting with
one of these letters (FIRST letter of variable name):
I J K L M N
ARE INTEGER VARIABLE
otherwise, they are implicitely
declared as REAL VARIABLE.
1) INTEGER :
2) REAL :
1) Integer conversions
2) Undeclared variables
3) Priority of operators
2**2**3 = 2^(2^3) = 2^8 = 256 4^3
B/C*D = (B*D)/C
4) Watch your punctuation !
The rules governing Mixed
Mode expressions are as follows.
1. Apply the priority rules for the selection of the arithmetic operations to be performed first.
( ) High priority
** Medium priority
* / Same priority
Operators with the same priority
in an assignment are processed from left to right.
2. If both of the selected variables
or constants are INTEGER, do the operation in INTEGER.
3. IF both of the selected variables
or constants are REAL, do the operation in REAL.
4. If one is INTEGER and one
is REAL, convert the INTEGER data to REAL and do the operation
Repeat the process until the expression is reduced to a single value. Note that internally, the computer must do arithmetic between data of the same type, both INTEGER or both REAL. Apart from these rules which apply to the evaluation of expressions on the right of the equal sign in an Assignment statement, there may still be conversion to a different type on the left of the equal sign.
! Arithmetic in print statements
PRINT *, '2+2 = ', 2+2
PRINT *, '2 ** 3 = ', 2**3
PRINT *, 'WITH REALS : ', 2.**3.
PRINT *, 'PARENTHESES : (-2+3)*(4+5) = ', (-2+3)*(4+5)
PRINT *, 'WITHOUT : ', -2+3*4+5
PRINT *, 'LOGIC : ', .TRUE., .FALSE.
PRINT *, 'CONTRADICT : (NOT TRUE) = ', .NOT. .TRUE.
PRINT *, 'AND : (TRUE AND FALSE) = ', .TRUE. .AND. .FALSE.
END PROGRAM ARITHMETIC
PROGRAM OUTPUT :
2+2 = 4
2 ** 3 = 8
WITH REALS : 8.00000
PARENTHESES : (-2+3)*(4+5) = 9
WITHOUT : 15
LOGIC : T F
CONTRADICT : (NOT TRUE) = F
AND : (TRUE AND FALSE) = F
General form :
Expression can be :
3) Combination of constants and variables
( combined with operators, ref.
to data type operators )
INTEGER :: AGE,PDS
REAL :: TOTAL, COST
CHARACTER (LEN=20) :: NAME
LOGICAL :: LOVED
AGE = 20
AGE = 20 + AGE
PDS = 10
TOTAL = (PDS*30.)/3
COST = TOTAL*1.13955
NAME = 'LUCIE'
LOVED = .TRUE.
END PROGRAM EXAMPLE
Go back to lecture menu
Go back to main page