We have encountered an IF statement in A Simple Program in Section 3 in the FORTRAN95 Manual. Section 6.1 has summarized all the forms of the IF construct. Details on the logical expression used in the IF constructs can be found in the section 4.7.
(i) Single statement.
IF
( logical expression ) statement
(ii) Single block of statements.
IF ( logical expression ) THEN
things to be done if true
END IF
(iii) Alternative actions.
IF ( logical expression ) THEN
things to be done if true
ELSE
things to be done if false
END IF
(iv) Several alternatives (there may be several ELSE Ifs, and there may or may not be an ELSE).
IF ( logical expression-1 ) THEN
.........
ELSE IF ( logical expression-2 ) THEN
.........
[ELSE
.........
]
END IF
Keyword: Go To line_number or GOTO line_number
Statements in a program are normally executed line by line in sequence. A GOTO statement will interrupt the sequence and re-direct the program to a new place. After a GOTO statement, the computer will execute the statement labelled by the line_number in the GOTO statement, instead of the statement immediate after the GOTO statement.
Exercises
Remember to add your name, group, course and date at the beginning of each program in comment lines.
1) (optional) Copy the quadratic
equation solver program Prog9 into U: drive, save it as Prog9.f95 and execute it.
Program
prog9
!=======
!
program to solve a quadratic
!
user types in the coefficients
!program
works out if roots are complex, real or coincident
!=======
!
ax*x
+ b*x + c =0
!
calculates b*b - 4(a*c)
!
Complex
croot1,croot2
Print
*,'program to find roots of a quadratic'
Print
*,'solves a*x*x + b*x + c =0 '
Print
*, 'type in three coefficients'
read (*,*) a,b,c
d=b*b
- 4*a*c
if (ABS (d) < 0.00000001) go to 101
if (d < 0.0) go to 100
!------------------
!
discriminant is positive so
square root ok
!------------------
root1=
(-b + SQRT (d))/(2.0*a)
root2=
(-b -SQRT (d))/(2.0*a)
print
*," root1 = ",root1
print
*," root2 = ",root2
stop
!------------------
!
complex roots - print out message
!-----------------
100
print *, ' roots are complex'
croot1=
CMPLX((-b/(2.0*a)), SQRT(ABS(d)))
croot2=CONJG
(croot1)
Print
*, croot1,croot2
stop
!------------------
!
roots are coincident - floating point arithmetic never
!
gives exactly zero
!--------------------
101
root1= (-b)/(2*a)
print *, " both roots are ",root1
stop
end
2) What happens if we type 0.0 for the first coefficient? Change the program to correct this.
3) List all the variables in Prog9 with their types.
4) List all the keywords used in Prog9.
5) List all the intrinsic functions used in Prog9.
6) How many single IF statements are there in Prog9?
7) Draw the flowchart of Prog9 (optional).
8) Read the following code, what will be the output if input a = 0, 1 or -1?
read(*,*) a
if
(a. lt. 0) a = -a
write(*,*)
a
9) The following program segment is for trapping unwanted input to
variable a. What is the valid data range of a?
100 read(*,*) a
if
(a.le.0) goto 100
write(*,*)
a
10) What does this
program segment do, which uses the IF construct with a
single block of statements? What will
be the output if a = 10, b = 100 or a = 100, b = 10?
Read(*,*) a, b
if
(a .gt. b) then
temp
= a
a
= b
b
= temp
end
if
write(*,*)
‘numbers are in ascending order’
write(*,*)
a, b
11) The following program
converts a mark into a grade. What are the output of the program for mark = 85,
73, 101, 60, 33, 55?
Read(*,*) mark
if
(mark.le.100.and.mark.ge.80) then
Write(*,*)
‘mark = ’, mark, ‘ Grade A’
else if (mark.lt.80.and.mark.ge.60) then
Write(*,*)‘mark
= ’, mark, ‘ Grade B’
else if (mark.lt.60.and.mark.ge.40) then
Write(*,*)‘mark
= ’, mark, ‘ Grade C’
else if (mark.lt.40.and.mark.ge.0) then
Write(*,*)‘mark
= ’, mark, ‘ Fail’
else
Write(*,*) ‘ incorrect mark’
end if
12) (optional) Write a program (if_Q12.f95), which uses
the IF construct with alternative actions
to calculate the value of the following function for x between -1 and 1. The
number of steps for x is entered via keyboard at the run-time of the program.
13) (optional) Write a program (stress.f95), which uses
the IF construct with several
alternatives to provide advice for a student who gives his present stress
level.
Input
a stress level (0-100)
Output
an advice according to the stress level:
0-40:
impossible.
41-80:
you are fine.
81-100:
see your personal tutor.
Other
numbers: input error
14) Write a program (checkout.f95) for supermarket checkout according to the flowchart.