Lesson 4 - Learning Goals

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

THE FOUR BASIC FORTRAN DATA TYPES

1. INTEGERS

• 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:

-       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 l0exp23)

• 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 -l0exp77 to +10exp77. 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:

&nbs\ p;  -       subtract

&nbs\ p;  *       multiply

/       integer divide : 17/5 = 3.4

** &bsp;    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 CHARACTER 8 BITS = 1 BYTE 8 BITS INTEGER 32 BITS 16 BITS REAL 32 BITS 32 BITS LOGICAL 1 BIT 1 BIT

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

.

.

.

MORE

FORTRAN VARIABLES

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 )

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

LOGICAL :: SINGLE

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 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):

I J K L M N

ARE INTEGER VARIABLE

otherwise, they are implicitely declared as REAL VARIABLE.

EXAMPLE :

1) INTEGER :

INUM

INDEX

LSUM

MONTH

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 Highest ( ) ** Right to left * / Left to right Lowest + - Left to right

2**2**3 = 2^(2^3) = 2^8 = 256 4^3

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

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