Title: ICS 101: Computer Programming in FORTRAN
1ICS 101 Computer Programming in FORTRAN
- Lecture 2 Logical Operations
2Lecture Objectives
- Logical Operators
- Relational Operators
- Logical Expressions
- Writing a Complete Program
3Logical Operators
- Basic logical operators .AND. .OR. .NOT.
- LOGICAL P, Q
4Logical Operators
- Precedence of logical operators
- Example .FALSE. .OR. .NOT. .TRUE. .AND. .TRUE.
.FALSE.
.FALSE.
.FALSE.
5Relational Operators
6Relational Operators
- .EQ. and .NE. operators may not work properly
with real values. This is because real values
cannot be stored exactly. - Example
REAL X, Y LOGICAL EQUALS PRINT, ENTER REAL
NUMBERS READ, X Y X (1.0 / X) EQUALS Y
.EQ. 1.0 PRINT, Y EQUALS 1? , EQUALS END
7Relational Operators
- Integers also affect the result of a relational
operator. - For integer K and L
- K / L L .EQ. K
- When k is divisible by L, the value of the
expression is .TRUE. Otherwise, the value will be
.FALSE.
8Logical Expressions
- A logical expression evaluates to either .TRUE.
or .FALSE. - It can have arithmetic, relational and logical
operators. - In a logical expression, evaluation is done in
this order - Arithmetic operators.
- Relational operators.
- Logical operators.
9Example
- X 3.0 Y 5.0 Z 10.0 FLAG .FALSE.
.NOT. FLAG .AND. X Y .GT. Z .OR. X Y .GT. Z
8.0
15.0
.TRUE.
.FALSE.
.TRUE.
.TRUE.
.TRUE.
10Writing Programs
- Read the problem carefully and identify the given
information and what the requirements (the final
results or output of the program) are. - Develop a solution for each requirement. To do
that, think about how this requirement can be
achieved. For example, a formula to find out the
output or the needed steps or strategy to get the
final result. - If there are more than one way to solve the
problem choose the best one in terms of cost and
time to do that solution. - Implement the solution in a program and test it.
11Example
- The Problem
- Write a program that computes the average of
three numbers entered by the user and then
display their average. - The solution
- The requirements are
- Three numbers are entered by the user.
- Compute the average.
- Display the average.
- Requirement solutions
- Use READ statement for entering the numbers.
- The average
- Use PRINT statement for displaying the average.
12Example
- Implementation
- All variables you need in the solution, declare
it in the beginning of the program. - Special statements should follow FORTRAN format.
- Any expressions needed should be converted into
FORTRAN format and gives the expected result. - Add comments to explain the purpose of the
program. - Add END statement at the end of the program.
- Testing
- Run the program and verify that it gives the
desired result. - If not, trace the running of the program and
discover any error.
13Example
Computer
C THIS PROGRAM READS 3 REAL C NUMBERS AND
COMPUTES AND PRINTS C THE AVERAGE REAL NUM1,
NUM2, NUM3, AVER PRINT, ENTER 3 NUMBERS
READ, NUM1, NUM2, NUM3 AVER (NUM1 NUM2
NUM3) / 3 PRINT, THE AVERAGE IS , AVER
END
NUM1
NUM2
8.0
9.0
NUM3
AVER
10.0
9.0
Output
ENTER 3 NUMBERS
9.0 8.0 10.0
THE AVERAGE IS 9.0
14Exercise
- What is the output of this program
- Assume the input is 3 2
- The output is 1.0 3 2 27
INTEGER XLM, NUM1, NUM2 REAL PNM READ, NUM1,
NUM2 PNM NUM1 / NUM2 XLM 3 / PNM 3.0
NUM2 PRINT, PNM, NUM1, NUM2, XLM END
15Exercise
- Convert the following into FORTRAN expression
- Write logical expressions for the following
conditions - A and B have the same sign (both are negative or
both are positive). - X is less than 3 or Y is less than 3 but not
both. - P Q R.
16Exercise
- The length of the line segment joining two points
(x1, y1) and (x2, y2) is given by - And the midpoint of the segment has coordinates
- Write a program that reads two points coordinates
and compute the length of the line segment and
the midpoint of the segment. Display these
results.
17Exercise
- Write a program that reads triples of real
numbers and assigns the appropriate value of true
or false to the following logical variables - TRIANG true if the real numbers represent
lengths of the sides of a triangle (the sum of
any two sides is greater than the third) and
false otherwise. - EQUIL true if equilateral (the three sides are
equal). - ISOS true if it is TRIANG and isosceles (at
least two sides are equal). - Sample output of the program should look like
ENTER 3 LENGTHS 2 3 3 TRIANG T EQUIL
F ISOS T