The Queen's University of Belfast
Parallel Computer Centre

[Next] [Previous] [Top]

Variables and Statements


Variables and Statements

Topics

Numeric data

Real and integer

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

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.

type [,attribute] :: variable list

REAL :: temperature, pressure

INTEGER :: count, hours, minutes

REAL :: temperature=96.4

INTEGER :: months=12, weeks=52

Explicit vs implicit declarations

a...h and o...z are REAL

i,j,k,l,m,n are INTEGER

Parameters

Specification

REAL, PARAMETER :: pi=3.141592

INTEGER, PARAMETER :: maxvalue=1024

INTEGER, PARAMETER :: repeat=1000

Arithmetic Expressions

Operators

+ Addition

- Subtraction

* Multiplication

/ Division

** Exponentiation

cost * number

cost * number + postage

10 + 3

4 * pi

1 / pressure

pi * radius * radius

Operator Precedence

Operator precedence.

Assignment Statement

=

variable = expression

pi = 3.141592

radius = 10.0

area = pi*radius*radius

Simple Input and Output

READ and WRITE

READ(5,*) radius

WRITE(6,*) area

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.

!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

WRITE(6,*) temp_value, &

pi*radius*radius,&

length, breadth

length=10.0; breadth=20.0

area= length*breadth

Derived Data Types

Definition and specification

TYPE :: name

component definition statements

...

END TYPE [name]

TYPE (name) [,attribute]:: variable

Example

TYPE rectangle

REAL :: length, breadth, area

END 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

TYPE rectangle

TYPE (point) :: centre

REAL :: length, breadth, area

ENDTYPE rectangle

Accessing Components

variable%component

rect_a%length = 10.0

rect_a%breadth = 20.0

rect_a%area = rect_a%length * &

rect_a%breadth

rect_a%position%x_coord = 5.0

rect_a%position%y_coord = 6.0

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