Computing Fundamentals
Fortran 77: Control Structures
Dr. Rizwan Ahmed
N-306 C, Faculty Bay 3
Department of Nuclear Engineering
Pakistan Institute of Engineering and Applied Sciences (PIEAS),
P.O. Nilore, Islamabad.
Office Contact: 2207381 (3750)
E-mail: rizwanahmed@pieas.edu.pk. rahmed.ne@gmail.com
Fortran Control Structures
IF statements
BLOCK
Arithmetic
IF-Else ….
Reference
Chapter 1 and Chapter 4,
W. E. Mayo, and M. Cwiakala, Theory and Problems of
Programming with FORTRAN 77, Schaum’s Outline
Series, 1995.
Programming and Problem Solving
Problem analysis and specification
Requirements (Inputs/outputs)
Algorithm design
Logic development (step-by-step procedure)
Program Coding
Writing a program in a programming language
Compilation and execution of program
Translation of program to machine language
Program maintenance
Debug and suggest improvements
Programming Tools
Building blocks
Used to develop a conceptual plan to tackle a problem
Sequential Execution
Instructions are performed one after the other
Loops
A block of instructions is repeated
Branching
A specific block of instructions is performed on the basis of
decision made
Example: Conditional
Example: Conditional
Programming Tools (cont’d)
Algorithm
Pseudocode
English like form which represent sequence of
instructions
Flow Chart
A flowchart is a visual or graphical representation of an
algorithm.
The flowchart employs a series of blocks and arrows to
represent operations or steps in the algorithm
Pseudocodes
x
i 1
i x1 x2 x3 .......... x N
Reference: R. L. Burden and J. D. Faires, “ Numerical Analysis, 9 th edition, Brooks/Cole, Cengage
Learning, 2011.
Algorithms
Algorithm
Procedure that describes a finite number of steps to be
performed in a specified order.
Reference: R. L. Burden and J. D. Faires, “ Numerical
Analysis, 9th edition, Brooks/Cole, Cengage Learning,
2011.
Flow Charts
Flow charts are used to represent algorithms in the form
of process flow diagrams
Reference: S. C. Chapra & R. P. Canale, “ Numerical methods for engineers, McGraw Hill, 2010.
Algorithms and Pseudocodes
Algorithms and Pseudocodes (cont’d)
Reference: S. C. Chapra & R. P. Canale, “ Numerical methods for engineers, McGraw Hill, 2010.
Algorithms and Pseudocodes (cont’d)
Fortran Control Structures
Selective Execution
Execution on choice/ condition
Statements: IF, IF-ELSE, IF-ELSE-IF
Logical expressions
Repeat a set of calculations
Statements: DO, IF and GOTO
Counted Loop
Conditional Loop
Logical Expressions
Logical expressions are used in IF statements.
Symbols Meaning Symbols
(old) (New)
A.LT.B A is less than B A<B or
A.GT.B A is greater than B A>B
A.EQ.B A is equal to B A==B
A.LE.B A is less than or equal to B A<=B
A.GE.B A is greater than or equal to B A>=B
A.NE.B A is not equal to B A/=B
Compound Logical Expressions
Compound logical expressions are formed by
combining two simple logical expressions.
Compound Expressions Meaning
(exp1).NOT.(exp2) TRUE when exp1 is true and exp2 is False
(exp1).AND.(exp2) TRUE when Both expressions are true
(exp1).OR.(exp2) TRUE when any one of the two is true
(exp1).EQV.(exp2) TRUE when both expressions are same
(exp1).NEQV.exp2) TRUE when both expressions are different
IF Statements
Selective execution of statements
Structures
BLOCK IF
IF-ELSE
IF-ELSE-IF
Arithmetic IF
Nested IF
BLOCK IF Structure
Syntax
IF(logical_expression)Arith.expression
Statements to be executed
….
BLOCK IF Structure
Syntax
IF (logical_expression) THEN
Statements to be executed
END IF
BLOCK IF Structure
Pseudocode Flow Chart
If GPA ≥3.0 then
Print ID
count ←count + 1
FORTRAN 77
IF-ELSE Structure
Syntax
IF (logical_expression) THEN
Statements to be executed
ELSE
Statements to be executed
END IF
IF-ELSE Structure
Pseudocode Flow Chart
If GPA ≥3.0 then
Print ID, GPA
count ←count + 1
Else
Print ID
FORTRAN 77
IF-ELSE IF Structure
Syntax
IF (logical_expression) THEN
Statements to be executed
ELSE IF(logical_expression) THEN
Statements to be executed
………
ELSE
Statements to be executed
END IF
IF-ELSE-IF Structure
Pseudocode Flow Chart
If GPA > 3.5 then
Print ID, GPA,‘*’
president’s count ←president’s count + 1
Else If GPA ≥3.0 then
Print ID, GPA
dean’s count ←dean’s count + 1
Else
Print ID
FORTRAN 77
IF ELSE-IF Structure (cont’d)
Example: Use of IF statement
Write a program to perform the following task
Reads a value for X as real number
Print “Hello here!” (if X <=50)
Print “Thank you very much” (if X <=100 and X>50), and
Print “Good bye” (if X >100)
Example: IF statement (cont’d)
PROGRAM
Example: IF statement (cont’d)
Output
Nested IF
Example
The Quadratic Equation
ax 2 bx c 0
2
b b 4ac
x
2a
Fortran’s Arithmetic IF statement
Syntax
IF (a)10,20,30
10 print*,’This is first block’
go to 100
20 print*, ’This is Second block’
go to 100
30 print*, ’This is third block’
100 continue
Fortran 90: SELECT CASE