You are on page 1of 10

AWESOME SUMMARY

FOR Csc211

&

Csc212

1
2
– a serial number is punched to identify the sequence of
statement in the card.
CSC212 Basic Elements of Fortran
FORTRAN – Integer Number: numbers without decimal points. i.e 210,
-302, +6580
– Real Number: numbers with exponentiation or decimal
History of Fortran points. i.e 34.98, 34e2 etc
– Developed by John Backus(a staff of IBM in 1957) (1954 -- – Double Precision variable: causes the fortran compiler to
1984) allocate 8 bytes of memory to every real variable and 4
– Initially for use on the IBM 704 computer bytes to very integer.
Note
Some Important Notes – for real variable in double precision is evaluated correct to
• fortran II/Fortran III — 1958 14(15) significant digits. i.e 3.66666666666666
• fortran IV — 1961 – single precision reads b/w 1 to 7 accurate digits. 3.666667
• Fortran 4 was released in 1957 – Character/string Variable: enclosed in double quotes.
• Study began on the first fortran standard in 1962 – Array Variable: a similar elements, which are numbers, or
• The first fortran standard (x3.9 — 1966) is often letters or both that have something in common. i.e A(10),
called FORTRAN 66 which had only 36 pages long. B(2,3)etc
— (1966) – Logical Variable: Name must be declared by using the
• FORTRAN 77 contained 300 pages plus about 70 reserved words LOGICAL i.e LOGICAL a, b, c
pages and 72 characters. – Complex Variable: has fractional and complex part. i.e 12
♦ One features of fortran 66 called + 12i
HOLLERITH was removed. — (1978) Rules For Variable naming
• FORTRAN 90 has mainly 400 pages, includes an 1. the first character must be an alphabet.
index, several appendixes and a long chapter on 2. No special characters
fortran intrinsic functions 132 Character length — 3. no spacing is allowed
(1992) 4. except in fortran 90, lowercase letters are not
• Fortran 95 — 1996 allowed.
• Fortran 2003 — 2004 5. In FORTRAN (IV) no. of char must not exceed 6.
• Fortran 2008 — 2010 other version allows 8 char and in fortran 90 allow
up to 31 chr.
Layout of Fortran Statement The underscore “_” is allowed as part of variable
Fortran data card which layout has been transformed name.
into the fortran coding environment in today’s computer Components of a Complete fortran program
consist of 80 column. – Program statement: is usually then first executable
it is a connection to use only column 1 — 72 statement which identify a program. It is optional
Note today the number of column in frortran environment form;
in “PC” is more than 80 PROGRAM program_name
Any statement that goes beyond column 72 – declaration of variable name;
cannot be executed. syntax;
Some Rules to Note Data type (variable_name)
– Column 1 or comment column: is left blank except when – open statement:
you want to make a comment. If “c” is typed in the column general form (syntax)
any information following thereafter is regarded as a OPEN (5, FILE=”variable name.DAT”)
comment . OPEN(6, FILE=”variable name.OUT”)
– Column 2 – 5 or Statement number: reserved for Note the first statement inform the compiler that an input
statement Number. If a five digit number is used, it should file called ‘variable_name.DAT’ stored in an input device
commence from column 1 and not extend to column 6. with device no. 5 exists. The first statement inform the
– Column 6 or continuation column: a delimiter such as *, compiler that an output file called ‘variable_name.OUT’ is to
(), & etc is typed in column 6 to inform the computer that be created on the hard disk.
the letter is a continuation of the former line. – Assignment statement: This is a method of instructing the
– Column 7 – 72 fortran executable statement: where any compiler to assign a value to a particular location.
executable statement is coded. – value assigned – right hand side
– Column 73 – 80 or identification section: – name of location – Left hand side
Note; th identification sequence is not a part of fortran – the read statement: An executable statement use in
statement. Rather it was used in identifying cards in the era feeding information into the memory location.
of using data card. – Unformatted read statement: not specified for the field
width. e.g

1
READ (*, *) variable_names DATA variable_name separated by
READ (5, *) variable_names comas/associated constant of either type.
Note the first asterisk specify that the data are to be i.e DATA ikp/’hello John’/ this will place the string Hello
entered from the keyboard. And the second is the device john into variable ikp
unit you assigned to the input device. Rules Governing the use of DATA Statement
– formatted Read statement: specified appropriate field 1. The variable name must agree with the associated
width of the location. e.g constant.
READ (*, 20) variable_names 2. Real variable name may be associated with con-
note the 20 connect the READ and the appropriate format stants in exponential form
statement 3. Implied DO loops are not allowed for assigning val-
– Write statement: instructs the compiler to print the value ues to variable names.
from the location which appeared after the write statement.
4. The DATA statement can be used with the CHARAC-
– Stop statement: it marks the logical end of a fortran
TER statement to assign character constants to var-
program.
– End statement; is the last executable statements which iable names.
indicate the physical end of a fortran program. - F-field specification: instructs the computer to start
Note a program with several subprograms will have one in printing the next field from the position w. it always act
each. like the ‘tap-stop’
– Character statement: informs the compiler that storage Tw
location in memory will store alphanumeric or string char Control characters:
and not numbers. e.g b Single spacing
CHARACTER V1*N1, V2*N2 … Vn*Nm O Double spacing
note _ Triple spacing
- Field width: specifies the number of char in a digit value | Skip to a new phone
that is used in reading in or writing out. + Do not skip to a new page
- Integer Field width I-field: generally written as iw, where;
/ Print what follows from
I implies that an integer is being read into or
printed out.
the beginning of a new
W number of digit or positions expected to be used. his.
“niw”
- Note: if the same integer field width is used for more than Arithmetic and logical operations
one integer, repetitive factor, n could be placed before I as - Arithmetic statement: the combination of operators and
niw. operands.
- Blank char space or field: instruct the compiler to leave i.e AI = A +(B+C) / D – E + f G ** H
char space of n number blank before printing or reading the - Arithmetic operators: are addition(plus),
next value. nx subtraction(minus), exponetiation(exponent) etc used to
- Real field width: are of two types namely the F-field and perform arithmetic operations.
E-field - Expression: combination of variable name of same type
 The general form of F-field/E-field is nfw.d/new.d using arithmetic operators.
Where; i.e A + (B - C) etc
N – is a repetitive factor - Integer arithmetic: if the operands are integers the
W – the number of digit including decimal point arithmetic will yield integer values.
D – the number of decimal places(fraction and i.e 2 + 3, 3 * 2
exponential part) - real arithmetic: if the operands are real will yield real
F – real field. values.
X – field width i.e 5.0 + 2.0. 5.0 * 2.9 etc
To obtain the least digits space to be provided for the E- Note
field , we require 7 + d position - two or more operands should not appear in succes-
- Logical or L-field specification: used to indicate that a true sion in an arithmetic expressions else the expres-
value if to be printed out from the corresponding logical sion is invalid.
variable. i.e nLw - When an expression contains operands of either
- A-field specification: indicate that an alphabetic char n type it is called a missed expression and its not al-
string and not numerals are to be read or printed out. lowed in fortran program
- DATA STATEMENT: feeding information into the computer. i.e John = A + 25 * Hope – Fear
Form - Fortran permit that the left hand side could be vari-
DATA variable_name/associated constants of either able of any type.
type Hierarchy of operations
Order expression is avaluated.
2
Highest () Sub expression - Flowchart a diagrammatic representation in logical block
: ** Exponentiation form of the flow of control within the program.
control statement
it’s the program structure in fortran
: *, / Multiplication / Divi-
1. Sequential structure
sion
2. Selection structure
: +, - Addition / subtration 3. Repetitive structure
: <<=>> Relational 4. Unconditional branching
= - Logical IF statement
: == != Equality General Form;
: .and. Logical AND IF (logical expression) statement
: .or. Logical OR IF (logical expression) GOTO statement number
Lowest = Assignment Unconditional GOTO statement
General Form;
- Arithmetic functions: built in, used for conversion of GOTO (statement number)
variable names of either type from one form to another. Arithmetic IF statement:
i.e FLOAT integer -- real General form;
IFIX real -- integer IF (arithmetic expression or variable_name )m1, m2,
INT real -- integer m3
AINT real -- real Where
Function for finding max or min of a number - m1 statement number if variable is less than 0
AMAX0 integer -- real - m2 if variable is equal to 0
MAX1 real -- integer - m3 if variable is greater than 0
MAX0 integer -- integer - block of structure:
AMAX1 real -- real general form;
AMIN1 real -- real IF (logical expression) THEN
MIN1 real -- integer Block of statement
MIN0 integer -- integer ENID
AMIN0 integer -- real - IF – Then ELSE structure:
General form;
- Modulus function: the remainder of the division of IF (logical expression) THEN
numbers by another. First block of statement
i.e N = (N/M) ELSE
MOD(N, M) = (N/M) * M Second block of statement
- Function references: also called mathematical function. ENDIF
i.e Tanx, Sinx, Cosx etc - IF – ELSE-IF structure:
note all real – real General form;
- logical operations: results in a truth value. i.e true or false IF (logical expression) THEN
- Logical operators: used in combining logical variable First block of statements
and constant. ELSEIF (second logical expression)
i.e .OR. .AND. & .NOT. Second block of statements
A1 A2 A1 .OR. A2 A1 .AND. A2 .NOT. A1 :
T T T T F :
T F T F F ELSEIF (logical expression)
last block of statements
F T T F T
ELSE
F T T F T Block of statements
F F F F T ENDIF
- WHILE DO statement:
NOTE the order if operation is General form;
.NOT. WHILE (logical expression) DO
.AND. Block of statements
.OR. ENWHILE
- NESTED IF-THEN structure
Arithmetic Development & flowchart pseudocode General form;
The itemization in a natural language, the various steps that IF (logical expression) THEN
will ne followed in order to process a given problem. Block of statement 1
:
3
: - The last executable statement is the return
IF (logical expression) THEN - Is invoked by the CALL statement whose general
Block of statement n form is
ENDIF CALL NAME (actual arguments)
ENDIF Difference between subroutine and subprograms
The Computed GOTO Statement 1. A subroutine can contain a stop statement
General form; 2. There can be more than one RETURN statement in
GOTO (S1, S2, … , Sn), K a subroutine
Where S1,S2,..,Sn are the associated statement numbers of
3. The RETURN can be placed any where in the sub-
n statement and k an integer variable. note if K = 3, control
routine
is transferred to statement number S3 and so on.
The Assigned GOTO statement 4. A subroutine can be called not only from the main
General form; program but from the subroutine.
GOTO K(S1,S2,…,Sn) ARRAY
Assign n to K An array is defined as a collection of similar elements which
Function subprograms and subroutines are numbers or letters or both that has something in
A subprogram is a complete and independent program common held under a variable. i.e B(20), A(4,5) etc
which can be used or invoked or called by the main Note array elements are referenced by means of a subscript.
program or other subprograms. Array may have more than one dimension. The bound
General from; of each dimension must be specified.
FUNCTION variable_name (list of arguments) Note two array are conformable if they have the same rank,
Body of subprograms size and shape their bounds may be different.
RETURN
END NOTE THE FOLLOWING EXAMPLE TO FIND RANK, BOUND
Note SIZE AND SHAPE
- main program is a calling program which invokes Example
other subprograms. Give the rank, bounds, size and shape of the arrays
- A variable name from the argument list of a func- defined as follows:
tion subprogram should not appear on the left-
(a) Real, DIMENSION (15) :: father
hand side of an equal sign within the subprogram
(b) Real, dimension (2:!2, 0:5) son
because it will change the value of the variable in (c) Integer, dimension (-2:2, 0:3, 6) :: grandson
the read statement of the calling program. (d) Real, dimension (3, 4, 2) mute
Merits of the Function Statement (e) Integer, dimension (0:2, -1:3, 4, 5) spawn
1. Simplifies the writing of a program as some compu-
tations could be written only once. Solution
2. Helps break a program into several smaller parts
Rules Governing the Use of Function Subprogram Answer
1. Must include FUNCTION followed by name of func- Rank bounds size shape
tion, list of arguments and enclosed in parenthesis. (a) 1 1:15 15 (15)
2. Function name must satisfy the rules governing the (b) 2 2:12, 0:5 66 (11, 6)
formation of a variable name (c) 3 -2:2, 0:3, 1.6 120 (5, 4, 6)
3. The subprogram must include a RETURN statement (d) 3 1:3, 1:4, 1:2 24 (3, 4, 2)
as the last statement executed. (e) 4 0:2, -1:3, 1:4, 1:5 300 (3, 5, 4, 5)
4. May contain any fortran statement
- Comments
5. The type of the function reference in the calling
Typing a ‘C’ in column 1, but in fortran 90 & recent ! is
program must be the same as the type function used in place of C.
6. The arguments when invoked must agree in num- - Implicit None: switches off the implicit type rule which is
ber, order and type with arguments in the function that variable name beginning with I,J,K L, M, N cannot be
statements. integer & other alphabets aside L, J, K, L, M, N cannot be
- Subroutine: is a sequence of commonly used operations real.
grouped in a form of subprogram to be used repeatedly
without duplications.
Note - A Runtime Error: Occurs while the program is running.
- Requires its first statement to identify its arguments - Compilation Error: state when a complier fails to compiler
- General form; code.
SUBROUTINE NAME (list of variables)

4
-- Execution Error: Occurs when program is given task it
cannot accomplish.
-- Syntax Error: An error in source code. CSC211
-- Logical Error: A mistake in a program’s source code that PASCAL
results in incorrect or unexpected behavior.
Note
- & is used to connect two lines.  Translators: A translator is a program which converts
- Fortran 90/95 code can start from any column. statements written in one language another.
While F77 and below starts at column 7.  Types of translators:
Logical Structure of Fortran Program • Compilers
1. Start of program • Interpreters
2. Reserved Memory • Assemblers
 Compilers: Translates programs written in “HLL” to
3. Execution part
their machine language programs equivalent. Converts
4. End program
Source program to object program.
Conversion of Variable  Editors: used at a terminal and for creation and
- Using intrinsic functions. INT, REAL, COMPLEX etc modification of programs.
X = REAL(K)/2  Debugging: locating and eliminating errors from a
N = 100*INT(X/1.5)+25 program.
Mixed-Type Assignment:  Programming Aims
<Real variable> = <integer expression> 1 Reliability
The RHS is converted to REAL 2 Maintainability
3 Portability
<integer variable> = <real expression> 4 Performance
The RHS is truncated to integer 5 Readability
6 Storage
Note for COMPLEX to REAL or INTEGER the  Stage Programming
imaginary part is discarded. • Understanding
• Planning the method of solution
• Development the method using suitable
methods, and notations
• Writing the instructions in programming
language
• Transcribing the instructions into a machine
sensible form
• Testing the subprogram separately and the
program as a whole
• Documenting all the work involved in
producing the program
• maintaining the program
Types of Error or Bugs During Testing
1 compile-time errors: Error during translation
2 Run-time errors: Error during execution.
3 Logical errors: Not detected during compilation or
execution. program runs successfully but yield
incorrect results

 Statements

An instruction or group of instructions that causes a


computer to carry out certain actions.

They are of two types

 Simple statement: simple unconditional


instructions that perform specific task.

 Structured statements: This includes; Compound

5
statement, Repetitive statement, Conditional – Boolean data type: include boolean-type constants,
statement. variables, expressions and functions.
Relational operators; = <> < <= > >=
Compound statement: consists of two or more Note False precedes True
consecutive statements. Note
Repetitive statement: involve the repeated  Or – expression wil be true if either or both
execution of several simple statements. operands is true
 NOT used as a prefix to negate a boolean operand.
Conditional statement: one or more statements  NOR will be true only if operands are true.
are executed only if some specific logical condition
is satisfied. – Standard Constants: are represented by standard
identifiers: maxint, false and true.
Procedures and Functions Note maxint denotes the largest value assumed by an
Are self-contained program elements, sometimes integer-type.
referred to as modules that carry out designated actions.
– Standard Functions: also called intrinsic or buit-in
Note: when a module is accessed, the information provided
functions.
is processed by the action statements within the module.
i.e Abs(x), Sqr(x), Char(x) etc.
Note standard functions are used with various simple data-
• Example of a standard pascal procedure is the write
types.
statement shown below
write(a,b,c);
Operators Precedence: The precedence groups are
Note: the a,b,c are parameters representing
tabulated below, from highest to lowest.
information being supplied to the procedure.
Precedence Operators
Highest NOT
• Example of a standard Pascal function is sqr used in * / DIV MOD AND
+ - OR
the following statement Lowest = <> < <= > >= IN
area := 3.141592*sqr(radius)
Note IN is a relational operator.
APPLICATIONS OF SIMPLE DATA TYPES There are seven 7 all together.
– Standard Simple-Type data: They are the integer, char, Parenthesis has the highest precedence.
boolean and real data. Assignment statement: used to assign data item to variable.
Note The first 3 are ordinal data type. i.e
The real is not an ordinal type. Variable := data item;
– Integer data type: They includes Constant, variable, Note the “data item” must be the same data-type as the
function, expression. variable to which its assigned.
Integer operators; + - * / DIV MOD
Note pascal does not support exponential operator.  Data input And Output
When using the MOD operator second operand must not be – Read statement: to read data items from an input
zero source.
using DIV will result in truncation towards zero. The syntax is
– Real data type: incudes constants, variable, functions, and read (input variable);
expressions. i.e read (a, b, c);
Real operators; + - * /
Note DIV and MOD cannot be used on real – The readln statement: Causes the next(not the current)
- Char data type: includes; single-character constants, read or readln statement to begin reading in a new line.
identifiers that represent single-constant, char-type whereas the read causes it to begin on same line.
variables, and certain char-type functions. – The EOLN and EOF Functions:
Note The following cannot be assigned to variables of the Note the EOLN(end-of-line) returns the boolean value true
type char-type if an end-of-line is encountered. It automatically reset to
‘ab’ ‘A q “b” false whenever a new line of input is started provided the
new line is not empty.
6
– the EOF(end-of-file) is used to detect the end of an input The REPEAT .. UNTIL Structure: loops that are repeated
file. To read unexpected number of data item from an input indeterminate number of times.
file. GEN Form
– The WRITE statement: used to write data item to the
output file. REPEAT sequence of statements UNTIL boolean
write(output data items); expression.
write(‘sum’, a+b);
Note integer data have a standard field width of 8, Note
while real have 14 with 7 digits to the right of decimal point – since the statement comes before the boolean expression
and boolean data have 6. in REPEAT … UNTIL the statement will be executed atleast
Note writeln statement results in an end-of-file destination once.
been written after the last data item. – While in WHILE .. DO the statement will not run until the
An empty writeln can be used to generate a blank line. expression is true.
– Formatted Output: To alter the default field width certain – REPEAT … UNTIL need not be included within BEGIN and
data item can be followed by a colon(:) and a positive END.
integer quantity which indicates the field width. i.e; – The FOR structure: Carry out unconditional looping,.
Allows actions to be repeated a specified number of
write (‘the answer is’, sum:4); times.
Forms;
for real values the first integer value after the colon denotes
the total field width and the value after the second colon 1. FOR control variable := value1 TO value2 Do
denotes the number of decimal point. i.e statement

write (‘sum is =’, sum:11:3); 2. FOR control variable := value1 DOWNTO value2 Do
will yield sum is = bb17487.266 statement

Note the decimal part will automatically be rounded or Note; the control variable automatically takes on its next
truncated if specification is too small. successive value each time the statement is repeated.
Note some char and what they do – if value1 and value2 are equal , the statement will be
– Blank space – print immediately after executed only once.
preceding – if value1 is greater than value2, the statement will not be
–0 – skip one line, then begin executed at all.
print (double spaced output) – for form 1. the control variable increase by 1. While for
1 – skip to the top of next page, form 2. the control variable decrease by 1.
then begin print – it is permissible to transfer control out of a loop
+ – print over the previous line prematurely but not permissible to transfer directly into the
– Control structure: loop from outside.
Note one compound statement can be embedded within – a loop may be nested completely within another loop.
another. – The IF structure:
ie forms;
BEGIN 1. IF boolean expression THEN statement
BEGIN 2. IF boolean expression THEN statement 1 ELSE
compound statement; statement 2
END Note – semicolon should not appear in an IF..THEN..ELSE
END. structure except as separators within a compound.
The WHILE … DO structure: is a repetitive control structure – IF structure can be nested within one another.
used to carry out conditional looping. – FOR control variable := value1 TO
GEN form value2 Do statement
WHILE boolean expression DO statement The Case Structure: allows some particular group of
Note The statement part will be executed repeatedly as statements to be chosen from several available groups.
long as the boolean expression remain true. General Form;

7
CASE expression OF VAR array name: ARRAY[index 1 type, index 2 type, …,
case label list 1 : statement 1; index n type] OF type;
case label list 2 : statement 2;
:
RECORD
: A structured data type whose constituents elements need
case label list n : statement n; not be of the same type.
END; Note data items are referred to by a simple identifier called
Note the expression can be any simple – type expression the record name.
other than real Gen form;
– the statement will be executed if and only if one of its VAR identifier: RECORD
Field identifier 1: type;
corresponding Case labels matches the current value of
Field identifier 2: type;
the expression. :
– Null(empty) statements are also permitted to indicate Field identifier n: type;
that no action is to be taken for certain values of the END;
selector. Record processing
– if the current value of the expression does not match any Gen form;
Preferred := regular
of the labels, then the action will be undefined.
– OTHERWISE clause specifies what action is to be taken if
Note its is more common to process record elements
the value of the expression does not match any of the individually rather than processing entire records. By
labels. forming a field designator, ie
– The GOTO Statement: used to alter the sequence of Record variable name.record field name
program execution by transferring control to some remote
part of the program. -- THE WITH Structure
Allows the record name to be omitted from the field
Gen form
designators.
GOTO statement label;
Note Gen form;
– statement label not greater than 9999. WITH record name Do statement;
Note: errors in programming are called bugs. WITH record 1 name, record 2 name, .., record n
Statement error is syntax error. name
-- user-defined simple – type data Do statement;
Enumerated – type data PROCEDURES AND FUNCTIONS
Consist of an ordered sequence of identifiers, where Pascal provides facilities which enable segmentation,
each identifier is interpreted as an individual data item. which permits the reuse of code without rewriting and
Gen form debugging it each time its is needed.
TYPE name = (data item 1, data item 2, …, data item n) Subprograms is called “procedure” or a “function”. It
Subrange – type data is invoked either by main program or by another
Some portion of the original range of an ordered, subprogram.
simple data type. Gen form of FUNCTION;
Note the original data type is refered to as, the host data FUNCTION name (variables) :type;
type -- invoking Function
Gen form; A function is invoked by execution of a function
TYPE name = first data item..last data item designator. Ie
Note the two consecutive periods (..) must separate the first Name(actual parameters)
and last items.
ARRAY Procedures
Consists of a collection of data item of same type. All Note PROCEDURES can compute more than one
items are refered to by a single identifier. results but FUNCTION cannot
Note each array elements are referred to by its subscript or Gen form;
indices PORCEDURE name (formal parameters; varying
Gen form; parameters);
1. One-dimensional form Body
VAR array name: ARRAY[index type] OF type; Invoking Procedures
2. Multi-dimensional form Is invoked by procedure statement ie
Name (actual parameters)

You might also like