The Queen's University of Belfast
Parallel Computer Centre

[Next] [Previous] [Top]

2 Variables and Statements


2.1 Data types

As humans we process various forms of information using senses such as sight, hearing, smell and touch. Much of the material produced by the academic community is processed visually, for example books, papers or notes. When processing numeric material we write the data as a stream of numeric characters, such as

365

96.4

3.14159

and then read the numbers as streams of characters. However in our minds we can think of each number as a numeric value rather than a series of characters. This is similar to the way a computer processes numeric data.

A computer programmer writing a program to manipulate numeric values uniquely
identifies each value by a name which refers to a discrete object remembered in the computer's memory. Thus the previous values may be identified by names such as:

daysinyear

temperature

pi

Note that it is good programming practice to use names that relate to the value that is being referred to.

There are two main forms of numeric data, namely INTEGER and REAL. Integers are essentially a restricted set of the mathematical whole numbers and as such may only have discrete values (i.e. no fractional part). The following are valid integer values:

-3124 -96 0 10 365

Real numbers may have a fractional part and have a greater range of possible values. For example:

10.3 -8.45 0.00002

There are two forms in which real values may be written. The examples above are in fixed point form but floating point form (similar to scientific notation) may be used, for example:

2.576x1032 1.3 x 10-22

may be written in Fortran as

2.576E32 1.3E-22

where the E stands for `exponent' or `multiplied by 10 to the power of'.

The integer and real values listed above, when used in a computer program, are known as literal constants.

Why reals and integers? Integers are more accurate for discrete values and are processed faster but reals are necessary for many scientific calculations.

In addition to the basic real and integer numbers there are other types of number such as double precision (which have more significant figures than the default REAL type) and complex numbers (with a real and imaginary part).

As well as numbers, Fortran programs often require other types of data. Single letters, words and phrases may be represented by the CHARACTER data type, while the logical values `true' and `false' are represented by the LOGICAL data type (details later).

2.2 Naming Convention

In Fortran objects are referred to by name. The naming convention permits names of between 1 and 31 alphanumeric characters (letters, numerals and the underscore character) with the restriction that the first character must be a letter. There is no case sensitivity in Fortran, the lower and uppercase versions of a character are treated as equivalent.

Unlike some programming languages in which certain words are reserved and may only be used by the programmer in precisely defined contexts Fortran has no reserved words. The programmer should take great care when naming objects not to use any words which form part of the language. In the course notes all words which have a defined meaning in the Fortran languages are given in uppercase and the user defined objects are given in lowercase.

2.3 Variables

Programs make use of objects whose value is allowed to change while it is running, these are known as variables. A variable must have an associated data type, such as REAL or INTEGER, and be identified at the start of the section of the program in which it is used (see later). This is referred to as declaring a variable, for example:

REAL :: temperature, pressure

INTEGER :: count, hours, minutes

declares five variables, two which have values that are real numbers and three that have integer values.

The variable declaration statement may also be used to assign an initial value to variables as they are declared. If an initial value is not assigned to a variable it should not be assumed to have any value until one is assigned using the assignment statement described below.

REAL :: temperature=96.4

INTEGER :: daysinyear=365, monthsinyear=12, weeksinyear=52

The general form of a variable declaration is:

TYPE [,attr] :: variable list

Where attr are optional Fortran 90 `commands' to further define the properties of variables. Attributes will be described throughout the course as they are introduced.

2.3.1 Implicit Declaration

Fortran 90 permits variables to be typed and declared implicitly, that is without using a variable declaration as given above. An implicit declaration is performed whenever a name appears which has not been explicitly declared and the program section does not contain the statement IMPLICIT NONE (see sample program). The implicit declaration facility is provided to comply with earlier definitions of the Fortran language and as this has been the cause of many programming problems this feature should be disabled using the IMPLICT NONE statement. Variables are typed according to the initial letter of their name: those beginning with I, J, K, L, M and N being integers; and those beginning A to H and O to Z being reals.

2.3.2 Parameters

The term parameter in Fortran is slightly misleading, it refers to a value which will be constant, for example the programmer will want the value of pi to be unaltered during a program. Therefore pi may be defined as

REAL, PARAMETER :: pi=3.141592

The word REAL defines the type of pi and the word PARAMETER is an attribute of the REAL object which is known as pi and has the value 3.141592. Parameters may also be defined for other data types, for example:

INTEGER, PARAMETER :: maxvalue=1024

INTEGER, PARAMETER :: repeatcount=1000

The objects declared to be parameters may not be altered in the program.

2.4 Arithmetic Expressions

Variables, parameters and numeric constants may be combined using the following operators:

+ Addition

- Subtraction

* Multiplication

/ Division

** Exponentiation

For example

cost * number

cost * number + postage

10 + 3

4 * pi

1 / pressure

pi * radius * radius

The expressions formed with arithmetic operators may be used in a variety of ways, one of which, the assignment statement, is described in the next section.

The arithmetic expression may also include brackets which should be used to clarify the required sequence of operations in an expression. For example:

pi*radius**2

might be interpreted as

(pi*radius)**2

The section of the expression which appears inside brackets is always evaluated first. In expressions which contain more than one operator the operations are carried out in an order which is determined by what are known as the "rules of precedence". The following table lists the priority or order of execution of the various operators.

The operators are evaluated in order of ascending precedence, that is, brackets first, then ** followed by * / and finally + -. Operators of equal precedence are evaluated working from left to right across the expression.

2.5 Assignment Statement

The expressions formed using arithmetic operators may be used to assign a value to a variable using the assignment operator, thus

area = pi*radius*radius

The assignment statement has the general form:

variable = expression

2.6 Simple Input and Output

On most computer systems the user can tell the program what values to perform a calculation upon by typing these at a keyboard. This is known as input and the values are assigned to the correct variables using the READ statement. The user will also wish to know the results generated by the program and this will usually be displayed on a screen using the WRITE statement - this is known as output.

To read in a value to say, a variable called radius, the following statement would be suitable

READ(5,*)radius

READ(*,*) radius

and the value of the variable area would be displayed on the screen by the following statement

WRITE(6,*) area

WRITE(*,*) area

The characters "(5,*)" should appear after every READ and the characters "(6,*)" after every WRITE (note that "(*,*)" may appear with either the READ or WRITE statements). The significance of these will be explained in a later section.

Several variables (or expressions) may be specified on one READ or WRITE statement as follows:

READ(5,*) length, breadth

WRITE(6,*) temperature, pressure, mass

WRITE(*,*) pi*radius**2, 2.0

2.7 Comments

All programs should have a textual commentary explaining the structure and meaning of each section of the program. All characters appearing on a line to the right of the ! character are ignored by the compiler and do not form any part of the program. The text appearing after a ! character is referred to as a comment and this feature should be used to explain to the reader of a program what the program is trying to achieve. This is particularly important if the program will have to be altered in the future especially as this is likely to be performed by a different programmer.

area = pi*radius*radius !Calculate the area of circle

Comments are also used to inhibit the action of statements that are used to output intermediate values when testing a program but which may be required again. The following statement is said to be commented out and is not executed.

! WRITE (6,*) temp, radius*radius

2.8 Program Layout

A sample program:

PROGRAM circle_area

IMPLICIT NONE

!reads a value representing the radius of a circle,

!then calculates and writes out the area of the circle.

REAL :: radius, area

REAL, PARAMETER :: pi=3.141592

READ (5,*) radius

area = pi*radius*radius

WRITE (6,*) area

END PROGRAM circle_area

There are a number of points to note in this program:

In general programs should be laid out with each statement on one line. However, there is an upper limit of 132 characters per line, (depending on the editor used it is often more convenient to keep to a maximum of 80 characters per line) a statement which exceeds the line limit may be continued on the next line by placing an ampersand & at the end of the line to be continued. The line should not be broken at an arbitrary point but at a sensible place.

WRITE (6,*) temp_value, pi*radius*radius, &

length, breadth

More than one statement may be placed on one line using a semicolon as a statement separator.

length=10.0; breadth=20.0; area= length*breadth

This is not recommended as it can lead to programs which are difficult to read - a statement may be overlooked.

2.9 Derived Data Types

2.9.1 Definition and specification

In many algorithms there are data items which can be grouped together to form an aggregate structure. A circle, for example may have the following properties:

radius

area

A programmer may define special data types, known as derived data types to create aggregate structures, thus a circle could be modelled as follows:

TYPE circle

INTEGER :: radius

REAL :: area

ENDTYPE circle

This would create a template which could be used to declare variables of this type

TYPE (circle) :: cir_a, cir_b

Just like the intrinsic data types, the components of a derived data type may be given an initial value. For example:

TYPE (circle) :: cir=circle(2,12.57)

The derived type is so named because it is derived from the intrinsic types, such as real and integer. However derived types may be used in the definition of other derived types. If a type, point, is defined

TYPE point

REAL :: x_coord, y_coord

ENDTYPE point

then the previously defined type, rectangle, could be modified to include a spacial position

TYPE circle

TYPE (point) :: centre

INTEGER :: radius

REAL :: area

ENDTYPE circle

The general form of a derived type definition is

TYPE type name

component definition statement

component definition statement

.....

END TYPE [type name]

This is a simplified version of the complete specification of a derived type, other elements may be added to this definition later. Note that the typename is optional on the ENDTYPE statement but should always be included to improve program clarity.

The general form of the variable declaration statement may be modified to included the specification of a derived type

TYPE [(type name)] [,attr] :: variable list

2.9.2 Accessing Components

The elements of a derived type may be accessed by using the variable name and the element name separated by the % character, as follows

cir_a%radius = 10.0

cir_a%area = pi * cir_a%radius**2

If a derived type has an element which is a derived type then a component may be accessed as follows

cir_a%centre%x_coord = 5.0

cir_a%centre%y_coord = 6.0

2.10 Exercises

  1. Which of the following values represent integers and which represent real numbers?

    0 1 1.2E-10 -1 -1.0

    0.0 0.1 1024 64.0 -1.56E12

  2. Which of the following are invalid names in Fortran and state why?

    abignumber thedate A_HUGE_NUMBER

    Time.minutes 10times Program

    1066 X HELP!

    f[t] no way another-number

  3. Given the following variable declarations and assignments evaluate the subsequent expressions and state the type of each result. Finally, insert brackets to clarify the meaning of these expressions according to the operator precedence table.

    REAL :: x=10.0 y=0.01, z=0.5

    INTEGER :: i=10, j=25, k=3

    i + j + k * i

    z * x / 10 + k

    z * k + z * j + z * i

    i * y - k / x + j

  4. Write definitions of derived types which represent the following

    (a) a point with x,y and z coordinates.

    (b) a time in hours, minutes and seconds.

    (c) a date in day, month and year.

    (d) a time comprised of the two types above.

    (e) a type containing 3 reals and 2 integers.

  5. Write a program which will read in two real numbers representing the length and breadth of a rectangle, and will print out the area calculated as length times breadth. (Use a derived type.)

  6. Write a program which will read in five integers and will output the sum and average of the numbers.


[Next] [Previous] [Top]
All documents are the responsibility of, and copyright, © their authors and do not represent the views of The Parallel Computer Centre, nor of The Queen's University of Belfast.
Maintained by Alan Rea, email A.Rea@qub.ac.uk
Generated with CERN WebMaker