The Queen's University of Belfast

Parallel Computer Centre
[Next] [Previous] [Top]
Variables and Statements
Variables and Statements
Topics
- Numeric Data.
- Variables.
- Parameters.
- Arithmetic Expressions.
- Assignment Statement.
- Simple Input and Output.
- Comments.
- Program layout.
- Derived Data Types.
Numeric data
Real and integer
- Numbers represented as characters
365 96.4 3.14159
daysinyear temperature pi
-3124 -96 0 10 365
10.3 -8.45 0.00002
2.576x1032 1.3x10-22
2.576E32 1.3E-22
Naming Convention
- Names upto 31 characters long:
- Letters a...z, A...Z,
- Numerals 0, 1, 2...9,
- Underscore _
- First character must be a letter.
- No case sensitivity.
- No reserved words.
- Examples:
X x1 mass pressure
1x _time ten.green.bottles
Variables
Declaration
A variable is a user defined entity whose value changes while a program runs.
- Declared at the start of the program.
- General form:
type [,attribute] :: variable list
REAL :: temperature, pressure
INTEGER :: count, hours, minutes
- Variables may be given a value on declaration:
REAL :: temperature=96.4
INTEGER :: months=12, weeks=52
Explicit vs implicit declarations
- All variables must be declared if the
IMPLICIT
NONE
statement is used.
- Otherwise a type is implied by the first letter
a...h and o...z are REAL
i,j,k,l,m,n are INTEGER
Parameters
Specification
- Parameters have a constant value.
- Requires the
PARAMETER
attribute.
- Examples:
REAL, PARAMETER :: pi=3.141592
INTEGER, PARAMETER :: maxvalue=1024
INTEGER, PARAMETER :: repeat=1000
- Parameters must be given an initial value.
Arithmetic Expressions
Operators
- Variable, parameters and literal numeric constants may be combined with the arithmetic operators:
+ Addition
- Subtraction
* Multiplication
/ Division
** Exponentiation
cost * number
cost * number + postage
10 + 3
4 * pi
1 / pressure
pi * radius * radius
Operator Precedence
- Expressions in brackets evaluated first
- Operators executed in order of ascending precedence
- Operators of equal precedence evaluated from left to right
Operator precedence.

- Example
- pi*radius**2
- (pi*radius) **2
- pi*(radius**2)
- a+b+c-d
- x*y/z+a
Assignment Statement
=
variable = expression
- Used to assign a value to a variable:
pi = 3.141592
radius = 10.0
- Used to assign the result of an expression to a variable:
area = pi*radius*radius
- The result of the expression should be the same type as the variable.
Simple Input and Output
READ and WRITE
- Input from keyboard via
READ
statement:
READ(5,*) radius
- Output to screen via
WRITE
statement:
WRITE(6,*) area
- Mutliple variables may be specified on the same
READ
/WRITE
statement:
READ(5,*) length, breadth
WRITE(6,*) temp, pressure, mass
WRITE(6,*) pi*radius**2, 2.0
Comments
Comments provide textual explanation of program statements and structure.
- Aid to understanding complex program sections.
- Characters following
!
are ignored by the compiler.
- May be used to temporarily remove statements when debugging by "comment out".
- Examples:
!this is a comment line
area = pi*radius**2 !Calculate area
!WRITE(6,*) temp, radius*radius
Program Layout
Sample program
PROGRAM Circle_area
IMPLICIT NONE
!This program 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 !answer
END PROGRAM Circle_area
General form
- Maximum of 132 characters per line.
- 80 may be a more practical limit.
- Extend on to next line using
&
WRITE(6,*) temp_value, &
pi*radius*radius,&
length, breadth
- Multiple statements per line using semicolon as a statement separator.
- poor programming practice.
length=10.0; breadth=20.0
area= length*breadth
Derived Data Types
Definition and specification
- An aggregate data structure, composed of:
- intrinsic data types,
- other derived types.
- General form:
TYPE :: name
component definition statements
...
END TYPE [name]
- Variables of type
name
can then be declared, general form:
TYPE (name) [,attribute]:: variable
Example
- To declare a data type representing a rectangle:
TYPE rectangle
REAL :: length, breadth, area
END TYPE rectangle
- The following would declare two variables of type
rectangle
TYPE (rectangle) :: rect_a, rect_b
Nested definitions
Derived data types definitions may include other derived data types.
TYPE point
REAL :: x_coord, y_coord
ENDTYPE point
- The previous definition of type
rectangle
may be modified to include a spacial coordinate, thus
TYPE rectangle
TYPE (point) :: centre
REAL :: length, breadth, area
ENDTYPE rectangle
Accessing Components
- Each component is accessed using:
variable%component
rect_a%length = 10.0
rect_a%breadth = 20.0
rect_a%area = rect_a%length * &
rect_a%breadth
- Nested elements are accessed thus:
rect_a%position%x_coord = 5.0
rect_a%position%y_coord = 6.0
- A derived data type may be given an initial value:
TYPE (point):: origin=point(0.0,0.0)
[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