**The Four Basic FORTRAN Data Types
**

- Represent discrete, exact numbers :

... -3 -2 -1 0 1 2 3 ...

- Written as a string of digits with optional sign.
- Can take all integer values within a certain range which depends on the machine, typically 32,768 to 32,767 (16 bits) or 2,147,483,648 to 2,147,483,647 (32 bits).
- Used in arithmetic expressions, and as subscripts.
- Basic operations:

+ add

- subtract

* multiply

/ integer divide : 17/5 = 3 (remainder of 2 thrown away)

** raise
to power = exponentiation

2. __REALS__

- Represent measured quantities, corresponding to real numbers.

.16 0. 00014 -0.3 6.02E23 (means 6.02 x l0^{23})

- Written as a string of digits with optional sign and mandatory decimal point, plus optional exponent (E followed by integer).
- Can represent broader range
of values, typically around -l0
^{77}to +10^{77}. Again, this depends on the machine. - Most quantities represented only « approximately .»
- Used in arithmetic expressions only.
- Real number with no fraction
(3.0 or 3.)
*not*the same as an integer (3)!!! - Basic operations:

+ add

- subtract

* multiply

/ integer divide : 17./5. = 3.4

** raise
to power = exponentiation

3. __CHARACTER DATA__

- Represent strings of one or more characters

'HELLO, WORLD' 'PROF. G. RATZER' '398-7082'

- Used for textual information, and for communication with user.
- Use two single-quotes to represent quote in string :

'"HOW''S IT GOIN'', EH?" '

4. __LOGICAL DATA__

- Can only represent one of two states:

.TRUE. .FALSE.

- Used to represent truth values and in decision-making.
- Basic operations:

.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

**Data Types
**

DATA TYPE | MAIN FRAME | MICRO |

1 BYTE | ||

**FORTRAN Also Allows
**

DATA TYPE |
SIZE |

.

.

.

MORE

**FORTRAN Variables
**

1- A quantity that can vary.

2- A location in memory.

3- A name used to refer to.

**Rules for Choosing a Name
**

A) 1 to 6 characters ( FORTRAN 77 )

or
1 to 31 characters ( FORTRAN 90 )

B) First character must be A
... Z, rest A...Z plus 0...9

__EXAMPLES :
__

TODAY SINGLE MAX MIN X ZMAT APOLLO13 -> NOT VALID IN FORTRAN 77 THISISTOOLONGTOBEAGOODVARIABLENAME -> NOT VALID 7UP -> NOT VALID !ALLO -> NOT VALID 22CATCH -> NOT VALID

**Declaration Statements
**

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

INTEGER :: K, PTR REAL :: SUM, COST, VALUE CHARACTER(LEN=20) :: NAME, ADDRSS LOGICAL :: SINGLEThe 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 part.

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.

**Implicit Declaration in FORTRAN
**

You should ALWAYS declare all
the variables you will use in your program.

If you forget, FORTRAN will assume
that variable names starting with one of these letters
(FIRST letter of variable name):

are __INTEGER__ variable otherwise, they are implicitly
declared as __REAL__ VARIABLE.

__EXAMPLE :
__

1) *INTEGER* :

INUMINDEXLSUMMONTH

2) *REAL* :

TOTAL

SUM

VALUE

TITLE

**Common FORTRAN ''Gotcha's''
**

1) Integer conversions

- Truncation ( I=2.89 I gets 2 )
- Integer division ( X = 2/3 X gets 0.0 )

2) Undeclared variables

- Wrong type
- Spelling mistakes

3) Priority of operators

Priority | Operations | Order |

2**2**3 = 2^{23} = 2^{8} = 256

B/C*D = (B*D)/C

4) Watch your punctuation !

DO J=1.10 PRINT *,J END DO

**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

+
Low 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
in REAL.

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 ! PROGRAM ARITHMETIC ! IMPLICIT NONE 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. STOP 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

**Assignment Statement
**

__General form :__

* Variable
= Expression
*

__Expression can be :
__

1)
Constant

2)
Variable

3) Combination of constants and variables

( combined with operators, ref.
to data type operators )

__EXAMPLE :
__

PROGRAM EXAMPLE IMPLICIT NONE 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.

STOP END PROGRAM EXAMPLE

On to the next lecture

Go back to lecture menu