You are on page 1of 14

NAME 336: Computer Programming in

Ship Design-I
Lecture-4
DR. MD. MASHUD KARIM
DR. ZOBAIR IBN AWAL
MD. HABIBUR RAHMAN
MD. MOINUL ISLAM

Department of Naval Architecture and Marine Engineering


Bangladesh University of Engineering and Technology

09/21/2020 1
Student learning is less effective when students sit
inertly in classes barely listening to teachers, passively
viewing PowerPoint presentations, memorizing pre-
packaged assignments, and spitting out answers.
Learning is not a spectator sport. Student learning is
optimized when students are actively involved in
their own learning. Students must talk about what
they are learning, write about it, relate it to past
experience, and apply it in their own lives. They must
make what they learn part of themselves.
Adapted from Chickering and Gamson, Seven
Principles for Good Practice in Undergraduate
Education
09/21/2020 2
Transfer of Control
The computer normally executes the instructions of a Fortran program
one line after another unless it is instructed otherwise. The order of
execution can be controlled by various instructions. Basically, there are
two types of transfer of control: unconditional and conditional.

Unconditional Transfer

The unconditional transfer of control can be accomplished by writing the


statement:
GOTO n

Where n is a statement number. This tells the computer to go,


unconditionally, to that part of the Program beginning with the
statement label n which is executable.

09/21/2020 3
Write a Fortran Program that generates and prints the
positive numbers.
Start
I =1
99 Print*, I
I = I+1
I 1 Goto 99
End

I =1
Print I 99 Write (6, 20) I
20
Format (1X, I4)
I = I+1
Goto 99
I I +1 End

09/21/2020 4
Start

Write a Fortran Program


Read ID,
that calculates average S1, S2, S3
marks of three subjects of
students. Write ID,
S1, S2, S3

SUM S1 + S2+ S3

AVE SUM/3

Write AVE
09/21/2020 5
Sample Program with Goto

Program average
100 Read*, id, s1, s2, s3
Print*, id, s1, s2, s3
sum = s1 + s2 + s3
ave = sum/3.0
Print*, “The average is:” ave
Goto 100
Stop
End

09/21/2020 6
Logical IF Statement
IF (Logical Expression) Statement A

Examples
IF (A.LE.B) GOTO 50 true
Logical
IF (L.GE.75) N = N+1
Exp.
IF (J.NE.K) Write(6,30) X, Y
false A

IF (Logical Exp.) Statement A


B
Statement B

If Logical Exp. is true then statement A is


executed; otherwise statement A is
skipped and B is executed

09/21/2020 7
Controlling a Loop
• Suppose we want to repeat a process (i.e., a set of
instructions) say 100 times.

• We can do this by using a counter I which counts the


number of times the process is repeated.

• That is we initially assign 1 to the counter, and then


each time the process is executed, we increment the
counter I by 1.

• If the counter I exceeds 100, we terminate the


execution of the process.

09/21/2020 8
Start

I 1

Process

I I+1

yes
I<=100

no

09/21/2020 Stop 9
Conditional Transfer
If we want to generate only
first one hundred positive I 1
integers, i.e., we want the
program to terminate after
Print I
generating the integer 100.

I I +1

yes
I <= 100

no

Stop

09/21/2020 10
Class Exercise -1
Suppose an amount of  3000.00 Tk is deposited in a
savings account in year 2000, and suppose the bank pays
6.5 percent interest on the account compounded
annually. Write a program which prints the YEAR and the
Amount of the account until 2020.

Clue:
Each year, AMOUNT is increased by 6.5 percent. Thus the
assignment,
AMOUNT  <- AMOUNT + 0.06*AMOUNT
Is repeated as long as YEAR <- 2020. 

09/21/2020 11
YEAR      <-     2000
AMOUNT   <-    3000.00

Print
YEAR,
AMOUNT

AMOUNT <- AMOUNT +


0.065* AMOUNT

YEAR  <- YEAR + 1

yes
YEAR <- 2020

no

Stop
09/21/2020 12
Class Exercise -2

Suppose the AGE of a man and the number of his


dependents NDEP are read from an input file.
Using logical IF statement calculate his income
tax deduction, DEDUCT, which is $750 per
dependent and show the results in an output file.
However if the man is 65 or older, then he can
claim an extra deduction (assume none of his
dependent is 65 or older).

09/21/2020 13
Class Exercise -3

Write a Fortran program to compute


average of three tests scores of 200
students using counter with input and
output file format.

09/21/2020 14

You might also like