Programming Language:FORTRAN
  • Basic Concept to FORTRAN
  • Structure of FORTRAN Program
  • Character Sets
  • Data types
  • Variables
  • Constant
  • Operators
  • Performing Arithmetic operations
  • Input / Output Statements
  • Control Structures:
    • Goto
    • Logical If
    • Arithmetic If
    • Do loops
  • Arrays
    • One dimensional array
    • Two dimensional array
Board Exam Theory Questions
  1. Formatted input / output statements Formatted input/output statements are read(*,*) and write(*,*) in fortran. The read(*,*) statement is used for reading data from input devices in user desire format. The write(*,*) statement is used to write/display data to output devices in user desire format.

    Syntax:
    read(UNIT=unit_no,FMT=format_no) list_of_variables_separated_by_comma
    write(UNIT=unit_no,FMT=formt_no) list_of_variables_separated_by_comma

    Here, unit_no is an integer that represents input or output device. A computer may have multiple input and output devices such as keyboard, magnetic tape, card reader, monitor, printer etc. In this case, a unique number is given to each device. An asterisk (*) in read() function represents stdin i.e. the keyboard and in write() function it represents stdout i.e. monitor. So, we can either specify a particualr device number in UNIT field or * for default device.

    The format_no refers to a label for a format statement. We can write an asterisk (*) to indicate default formatting.

    Example of read and write with default unit and default format:
    read(*,*),list_of_variables_separated_by_comma
    write(*,*),list_of_variables_separated_by_comma

    Here, comma at beginning of the variable-list is optional.

    c Example of Formatted input/ouput
    program addition
    integer a,b,c
    write(*,*)'Enter any two numbers'
    read(*,*)a,b
    c = a + b
    write(*,*)'Required addition is ',c
    end

    Unformatted input / output statements Unformatted input/output statements are read*, and print*, in fortran. When unformatted input/output statements are used, the computer itself decides the formats.

    c Unformatted input/ouput
    program addition
    integer a,b,c
    print*,'Enter any two numbers'
    read*,a,b
    c = a + b
    print*,'Required addition is ',c
    end
  2. Formatted input and output statements
    Formatted input/output statements are read(*,*) and write(*,*) in fortran. The read(*,*) statement is used for reading data from input devices in user desire format. The write(*,*) statement is used to write/display data to output devices in user desire format.

    Syntax:
    read(UNIT=unit_no,FMT=format_no) list_of_variables_separated_by_comma
    write(UNIT=unit_no,FMT=formt_no) list_of_variables_separated_by_comma

    Here, unit_no is an integer that represents input or output device. An asterisk (*) represents default device that is keyboard in read() and monitor in write() statements.

    The format_no refers to a label for a format statement. We can write an asterisk (*) to indicate default formatting. However we can specify some particular input or output format, e.g. how may decimals in real numbers. For this we use FORMAT statement.

    Syntax of FORMAT statement is: N FORMAT(f1,f2,f3,...fn) where
    • N is the statement number and must be for each format statement. It can be placed anywhere in the program.
    • f1,f2,f3,...fn are the format specifications and these must be seperated by commas and enclosed within parenthesis.

    The most common format code letters are:
    Name Description General Format
    F Format The symbol F is used to denote the real data expressed in decimal form. Fw.d where w is field width and d is precision.
    A Format The symbol A is used for character type data. Aw where w is the width which is optional.
    I Format The symbol I is used to denote the integer data. Iw where w is the width of the integer data.
    E Format The symbol E is used to denote a real data expressed in exponential form. Ew.d where w is the total field width including mantissa (i.e. base) and d is the decimal width (i.e. precision).
    X Format The symbol X is used to skip some columns while printing data. nX where n is the number of columns to be skipped in case of reading data from keyboard.
    T Format This format is used to give tab. Tn where n specifies the number of column form which the output will be started.

    c Formatted input/ouput Example
    program i/o example
    real pi
    write(*,*)'Enter the value for pi'
    read(*,101)pi
    write(*,102)'pi=',pi
    101 Format(F8.6)
    102 Format(A5,F6.4)
    End

    Output
    Enter the value for pi
    3.141592
    pi=3.1416
  3. How computer programming language C is different from FORTRAN? [4] [067]
  4. What are the characteristics of FORTRAN programming? [3] [074]
  5. What are FORTRAN’s constants, variables and library functions? Explain. [4] [067]
  6. What is the structure of FORTRAN program? [5] [073] [068]
  7. Explain the FORTRAN structure. What are data types in FORTRAN. [2+2] [070]
  8. Compare arithmetic and logical if statement in FORTRAN. [7] [071]
  9. Differentiate between arithmetic and logical if statement. [2+2] [068]
  10. Compare arithmetic and logical if statements in FORTRAN with suitable example. [4] [070]
  11. Differentiate “Logical if” with “Arithmetic if” in FORTRAN with suitable example. [6] [069]
  12. Compare unconditional goto and computed goto in FORTRAN with syntax. [3] [073]
  13. Explain different types of goto statements in FORTRAN programming with suitable example. [4] [072]
  14. Compare “Computed goto” statement (FORTRAN) and “switch” (C language). [069]
  15. How are do-loops used in FORTRAN? Explain with example. Compare it with implied do-loop. [2] [069]
  16. Explain with suitable example to show how an Implied Do loop works in FORTRAN. [4] [072]
  17. Compare DO and implied DO statement in FORTRAN. [3] [072]
  18. What is the syntax of two dimensional arrays in FORTRAN? Explain with example. [4] [069]

FORTRAN

FORTRAN stands for FORmula TRANslation. It is one of the oldest computer programming languages, developed in 1954, by John Backus for IBM. It is most widely used high level programming language and is being used mainly for mathematical or numerical or scientific applications. The history of FORTRAN’s versions can be classified as follow.

  • FORTRAN 1957
  • FORTRAN II
  • FORTRAN IV
  • FORTRAN 66
  • FORTRAN 77
  • FORTRAN 90
  • FORTRAN 95

And the most common and popular version of FORTRAN today is still Fortran 77. [A Brief History of FORTRAN]

ForTran is included in the curriculum of TU, IOE, BE (all programs) as topic of syllabus of COMPUTER PROGRAMMIG. The weight of this topic is 12 out of 80 marks and the allocated time duration is 8 hours. According to the syllabus, we should study about C programming language before studying ForTran. Concept of writing algorithm, drawing flowchart, flowcharting symbols and fundamental of programming is independent of any languages. It means, we are already familiar to those common topics. Therefore, in this text we mainly focus on the syntactic structure of ForTran programming language. Let us compare a basic example of a ForTran program to C program. We can write ForTran syntax either in all upper case or all in lower case or in both.




FORTRAN Program Structure

  • Documentation section
  • Program name
  • Declaration of variables
  • Variable initialization
  • Program statement
  • End
Documentation section:
The documentation section tells the user what the program does, who wrote it, dates written and revision history. For documentation, we can use either c,* or !. To use C or * for commenting it must be placed at the first column of the ForTran coding sheet but symbol ! can be placed anywhere except inside single quotes.

Program name:
The first line of the programs generally gives the program’s name. For example, program sum indicates that the following program is about addition. Here program is a ForTran verb and sum is identifier. We can write whatever we like. In fact, this part of a ForTran program is optional. But it is good practice to name every program.

Declaration of variables:
In this we must declare all the variables, which set up space in computer memory for the data types that will be used in the program.

Variable Initialization:
After the declaration of variables, our computer knows how much space is to be allocated for each variable, but the actual value of this variable is not known. Although some ForTran77 compilers implicitly assign each declared variable a zero value or an empty string (for character types), it is strongly recommended to initialize variables if any variable needs to set some initial value.

Program statement:
Program statements are the actual steps that solve the original problem. Comment lines can also be included in order to describe what the line of code actually does.

End:
End is the last statement of the ForTran program. It indicates the end of the program. Every ForTran program must be terminated by the end statement.



ForTran Coding Sheet

It is a sheet of paper used to write ForTran program.
While writing FORTRAN statements, it requires text and statements to be placed in specific area of each line. Why?

  • It has 80 columns.
  • Each line is divided into five distinct zones as described below.
  • Column 1: Used for writing comment. Character c or * indicate the line as comment line.
  • column 1 – 5: Used for giving line numbers for other lines of program. The line number is a positive integer with at most five digits. Every statement need not be given the line number.
  • column 6: Used for continuation i.e. if a statement does not fit in columns 7 to 72, the remaining part must be written in the next line. We can use + sign as a continuation mark in position 6 which means both lines represent single statement, not two separate statements. Any character can be used instead of the plus sign as a continuation character. It is considered good programming style to use the plus sign or an ampersand or digits (using 2 for the second line, 3 for third, and so on).
  • column 7 – 72 : For writing ForTran statements.
  • column 73 – 80 : Used for comment for the programmer if needed. The compiler does not read columns 73 to 80.


Character Set

The character set includes letters from A to Z or a to z. Similarly, digits from 0 to 9 and symbols +-/*.,’_$().


FORTRAN Verbs / Keywords

What is the role of keywords in C? There are some words in ForTran having the similar role are called ForTran verbs. For example, program, character, integer, real, double precision, if, then, else, while, end etc.


Data Types

ForTran program supports following standard data types.

Data Type Size Description Example
character 1 byte Represents one alphanumeric character. The character*N represents multiple characters. WAP to Enter your name and display it?
integer 4 bytes Represents both positive and negative numbers. (non-fractional) WAP to add two numbers given by user?
real 4 Represents both positive and negative numbers with a fractional part. WAP to find area of a circle using real datatype?
Double precision 8 bytes Same as Real but using twice the storage space and greater precision. WAP to find area of a circle using double precision datatype?
logical 1 bit Two logical values: .true. (equivant to T or 1) and .false. (equivant to F or 0) WAP showing example of logical datatype?
complex 2*4 bytes Represents real and imaginary part. WAP to add two complex number?


Constants

Constant has a value which is fixed when the program is written. It is called constant as its value doesn’t change during program execution.

Integer Constant
Integer numbers written without decimal point are called fixed-point constants or integer constants. Integers are formed by digits 0,1,2,3,4,5,6,7,8,9 and + or – symbol. The sign symbol occurs at the leftmost position of the number. If the number is positive the plus sign is optional.
For example: +1234, 420, -10

Real Constant
If a numbers has a fractional part is called real or floating point constant. Such numbers are written with one decimal point.
For example: 3.1415, 2.5, 1.123E13

Character Constant
These constants are similar to the string constants in C. But in case of ForTran these constants are enclosed by single quotation marks.
For example: ‘Patan’, ‘Pulchowk campus’

Logical Constant
There are only two possible logical constants, and they are expressed as: .TRUE. and .FALSE. The dots at each end are needed to distinguish these special forms from the words TRUE and FALSE.

Complex Constant
A complex constant has real and imaginary component separated by a comma and enclosed in a pair of parentheses.
For example: (2.14, -3.67) (2,4)


Variables

The concept of variable is already studied in C. Actually variable are the name of memory location where the data are stored for computing. The first character of any variable name must be an alphabet. ForTran verbs cannot be used for variable. Similarly, any special characters are not allowed in variable names. For example, 7sum, goto, r-e are some invalid variable names. It is better to give meaningful variable name. Example:
integer i,j
real area
character name*20


Defining data types for a variable

i. Explicit definition
We can define variable explicitly by using the data type like:
integer i,j,k
real x,y

ii. Implicit definition
We can define a variable without explicit data type in variable declaration statement. Older versions of ForTran allowed a feature called implicit definition, i.e. you do not have to declare the variables before use. If a variable is not declared, then the first letter of its name will determine its type.

Variable names starting with i,j,k,l,m,n are considered to be for integer variable and others are real variables. However, you must declare all the variables as it is good programming practice. For that you start your program with the statement – implicit none. This statement turns off implicit definition.

Q. WAP showing example of implicit definition?

Operators and Expression

Operators are used in programs to perfrom certain mathematical or logical manipulations. For example, in a simple expression 8+9, the symbol + is called an operator which operates on two data items 8 and 9. And the data items that operators act upon are called operands. So here, 8 and 9 are operands.
An expression is a combination of variables, constants and operators written according to syntax of the language. For example, the following are examples of expressions:
9 + 8, a+b*c, a>b, a*b/3

Types of Operators


Types of Operator Example
1. Arithmetic Operator WAP showing example of Arithmetic Operators (+,-,*,/,**)
2. Relational Operator WAP to find greater number among any two numbers?
3. Logical Operator WAP to find greater number among any three numbers?
4. Assignment Operator WAP showing example of Assignment operator?
5. String Concatenation Operator WAP to concatenate two string?

1. Arithmetic Operators

The arithmetic operator perform arithmetic operation.

Arithmetic Operators Purpose
+ Addition (a+b)
- Subtraction (a-b)
* Multiplication (a*b)
/ Division (a/b)
** Exponentiation (n**2)

2. Relational Operator

The relational operators are the symbols which is used to compared two or more subject matter.

Relational operator FORTRAN operator Meaning
< .LT. (smaller than) a .lt. 5
> .GT. (greater than) a .gt. 5
<= .LE. (smaller than equal to) a .le. 5
>= .GE. (greater than equal to) a .ge. 5
== .EQ. (equal to) marks .eq. 32
!= .NE. (not equal to) mark .ne. 32

3. Logical Operator

The logical operator are used to combine two or more than two expression of relational operator.

Logical operators Purpose
.AND. if(a .GT. b .AND. a .GT. c)
.OR. if(marks .LT. 32 .OR. attendance .LT. 80)
.NOT. .NOT. .TRUE.

4. Assignment Operator

Assignment operator are used to assigne a value of right hand variable to left hand variable.
Example:
A = l x b
sum = a + b

5. String Concatenation Operator

Two Character strings can be joined together into a single string using concatenation operator.
The concatenation operator is represented by a double forward slash //.
It is also known as character operator.


Control Statement in FORTRAN

The statement which alter the flow of execution of the program are known as control statements. The control statement may be either decision making or loop / repetition.


Decision Making Statements

The decision making statement decides to execute certain statements or not. The decision making types of control statements are classified as following:

  1. Logical if (if else, if else ladder, nested if else)
  2. Arithmetic if
  3. Unconditional goto statement
  4. Computed goto statement

i. Logical if

The concept of logical if is similar to if (along with if else, if else ladder and nested if else statement) statement in C programming. The general form of this statement in FORTRAN is:

if (test condition) then
statement 1
… …
else
statement 2
… …
endif

If condition produces true value, the statement 1 is executed, otherwise statement2 is executed.

ii. Arithmetic if

The arithmetic if statement is used to transfer the program control to a particular statement in the program depending upon the value of an expression whether it is negative, zero or positive. The general form of Arithmetic if is:

if (expression) st_no1, st_no2, st_no3

where expression is any valid FORTRAN arithmetic expression and stn1, stn2, and stn3 are the statement numbers.

If the value of the expression is negative, control goes to the statement number st_no1,
If the value of the expression is zero, control goes to the statement number st_no2
If the value of the expression is positive, control goes to the statement number st_no3


iii. Unconditional goto statement

This statement is used to transfer the program control to any other statement unconditionally. Its general form is:

goto st_no

where, st_no is the statement number where the program control is transferred to any number of statement without checking any condition. The value of st_no must not be floating point number, zero or negative. It can be five digits at most. There must be one statement number for each statement.

iv. Computed goto statement

The computed goto statement transfers program control to one of several statements specified, depending on the value of an integer. The general form of this type of goto statement is:

goto (st_no1, st_no2, st_no3,…st_n), i

where, st_no1, st_no2, st_no3 … n are statement numbers. These must be enclosed within parenthesis. The variable i is the single integer variable. There must be a comma between closing parenthesis and the integer variable like i.

if i = 1, control goes to statement number st_no1
if i = 2, control goes to statement number st_no2
if i = 3, control goes to statement number st_no3
… …
if i = n, control goes to statement number st_n

It is important to note that if i < 1 or i >n, the goto statement is ignored and control transfers to next statement line.

Looping / Repetition in FORTRAN

Loop may be defined as block of statements which are repeatedly executed for a certain number of times or until a particular condition is satisfied. The FORTRAN 77 has only one loop: do loop, supported by ANSII standard. However most of compilers supports following types of loops.

  • i. do-loop (like for loop in C)
  • ii. do-while loop (like while loop in C)
  • iii. do-until loop (like do while loop in C)

i. do-loop (like for loop in C)

The do loop is used for executing a block of code repeatedly for pre-determined number of times. It is also known as controlled loop. It is considered as the most effective loop in FORTRAN and it is exactly same as for loop in C programming. The general form of do loop is

do [st_no] [variable = initial_value], [final_value], [step_size]
statement 1
statement 2
… …
statement n
st_no continue

The st_no (statement number) represent the last statement of the body of the loop. The final statement may be any executable statement but is usually a CONTINUE statement.
Variable may be any type INTEGER, REAL OR DOUBLE PRECISION which is initialized with initial_value, from which the loop execution begins.
The initial value is increased by step_size and it is increased/decreased until it reaches to final_value. When the loop-control reaches to final_value, the control is transferred to statement number specified by the st_no.

Here, step_size and st_no are optional and default value for step_size is 1. The statement enddo can be used to determine the end of do loop instead of using st_no and continue statement.

ii. do-while loop (like while loop in C)

The do while loop cycles through statements as long as a given condition is true.

do while(i .lt. 5)
i = i + 1
write(*,*)i
end do

However, FORTRAN does not have a formal do-while loop structure. Even though this syntax of do-while loop is accepted by many compilers, it is not ANSI FORTRAN 77 standard.

iii. do-until loop (like do while loop in C)

If the termination criteria is at the end instead of the beginning of the loop, it is often called an until-loop. In until loop, the body of the loop is executed first without testing condition. At the end of the loop, test condition is evaluated.

do
write(*,*)i
i = i + 1
until(i . lt. 5)

However, FORTRAN does not have a formal until loop structure. Even though this syntax of until loop is accepted by many compilers, it is not ANSI FORTRAN 77 standard.


Array

An array is a group of related data items that share a common name. In other words, an array is data structure that store a number of data items as a single entity (object). The individual data items are called elements and all of them have same data types. We have learned different array processing in C. The theoretical concept is same to that. Here, we study how to manipulate arrays using FORTRAN syntax.

One dimensional array

The general form of one dimensional array is

Syntax: data_type array_name(array_length)

Example: integer num (5)

Here, array_name is the name of the array and the array_length is the number of elements in the array. Array indexing in FORTRAN starts from one and ends with array_length unlike C.

Implied Do loop
An implied do-loop is a special form of a loop that can be performed on one line. This can be useful for accessing elements in an array. For example, assuming i is declared as an integer, the following code,

write (*,*) (a(i), i=1,5)

Comparison between normal and implied do loop structure
Normal do loop Implied do loop structure purpose
do 1 k=1, 5, 1
read(*,*) a(k)
1 continue
read(*,*) (a(i),i=1,5) To read data for array
members a(1), a(2),
a(3), a(4), and a(5)
do 2 k=1, 5, 1
write(*,*) a(k)
2 continue
write(*,*) (a(i),i=1,5) To display content of
array members a(1),
a(2), a(3), a(4), and a(5)


Two dimensional array

Two dimensional array is array with two subscripts. The two dimensional array is defined as

Syntax: datatype array_name(dimension1, dimension2, ... , ...)

Example: integer a(3,3)

BRANCHING STATEMENT

LOOPING STATEMENT

1D ARRAY

2D ARRAY

Web hosting by Somee.com