Your Federal Quarterly Tax Payments are due April 15th

# 1 2. IF Statement -Using IF statements in Fortran-IV and Fortran by NickyvGraham

VIEWS: 9 PAGES: 4

• pg 1
```									CMPE 108 Algorithms and Programming, by Dr. M. Bodur,

Spring 08-09, Experiment #2 EMU, CMPE Dept. CMPE 108 Algorithms and Programming

1

2. IF Statement
2.1 Objective:

-Using IF statements in Fortran-IV and Fortran 90 programs. -Practice of a writing, compiling and running a program with various types of IF statements.
2.2 Outcomes:

On successful completion of this experiment you will experience and know -how to write a Fortran arithmetic if statement into a Fortran program file
2.3 Preliminary Information

2.3.1 Arithmetic IF statement An arithmetic if statement has the following prototype IF (variable or Arithmetic expression) n1, n2, n3 Control is transferred to the statement numbers n1, n2, n3, depending on the value or result of the variable or arithmetic expression in the parenthesis. A negative value transfers the control to n1, a zero transfers the control to n2, and similarly, a positive value transfers the control to n3. Example:
! calculate the discriminant. D=B*B-4.*A*C IF(D) 15, 25, 35 15 WRITE(*,*) ‘Roots are Complex numbers” ... 20 WRITE(*,*) ‘Roots overlapping’ ... 25 WRITE(*,*) ‘Roots positive’ ...

2.3.2 Logical IF statement A logical if statement consists of two parts: a logical expression in the parenthesis, and a consequence expression following the parenthesis. IF (logical_expression) consequence_expression If the logical expression is satisfied the consequence expression is executed, otherwise control is transferred to the next statement skipping the then part of the if statement. A logical expression is built by relational and logical operators. 2.3.2.1 Relational Operators A relational operator compares numerical values of its first and second operands A and B, which may be real or integer expressions: A .EQ. B results in logical true only if A is equal to B. A .NE. B results in logical true only if A is not equal B. A .GT. B results in true only if A is greater than B. A .GE. B gives true only if A is greater than or equal to B. A .LT. B gives true only if A is less than B. A .LE. B gives true only if A is less than or equal to B

A False False True True
A

B False True False True
B

A .OR. B False True True True
A .AND. B

False False True True

False True False True
A False True .NOT. A True False

False False False True

CMPE 108 Algorithms and Programming, by Dr. M. Bodur,

Spring 08-09, Experiment #2

2

Relational operators work on real or integer operands, and results in a logical value: either True, or False. For example, ( A .EQ. 0.5 ) is true only if the value of A is exactly equal to 0.5. Similarly, B .LE. A is true only if B is less than or equal to A. 2.3.2.2 Logical Operators Three main logical operators we use frequently are .NOT. .OR. .AND.. These three operators are easily defined by their truth tables. The relational expressions can be combined with logical expressions to give a logical expression. Example:
IF(A.LT.B .AND. A.NE.0) Z= B/A

will calculate Z only if both A is less than B, and also A is not equal to zero.
2.4 Experimental Activity

Draw a flow chart and write a computer program in Fortran to do the followings: - Read values of x, y, z from one line of text entered by the console keyboard. - Calculate value of g= z2 – 2xy . - Write the following texts to console monitor. if g is positive, “g is positive” ; if g is zero “no solution”; if g <0, “g is negative”. a) using arithmetic if statements b) using logical if statements Solution Flowchart

start Read x, y, z, g= z2 – 2 xy g>0 g (20) g=0 Write “g is positive”
(30) (10)

g<0

Write “no solution”

(40)

Write “g is negative” stop

Fortran95 program with arithmetic if statements
! ARITHMETIC IF STATEMENTS PROGRAM AIF REAL x,y,z,G OPEN (8,FILE='Exp2in.txt') OPEN (7,FILE='Exp2Aout.txt') READ(8,10) X,Y,Z 10 FORMAT (3(2X,F6.2)) G=Z*Z-2.*X*Y WRITE(*,*) G IF(G) 20,30,40 20 WRITE(7,12) 12 FORMAT(5X,'g is negative') GOTO 50 30 WRITE(7,13) 13 FORMAT(5X,'no solution') GOTO 50 40 WRITE(7,14) 14 FORMAT(5X,'g is positive') 50 READ(*,*) ; STOP END PROGRAM AIF

CMPE 108 Algorithms and Programming, by Dr. M. Bodur,

Spring 08-09, Experiment #2

3

Fortran95 program with logical if statements
! Logical if statements ! IF( X ) 10,20,30 == IF(X<0) GOTO 10 ! IF(X==0) GOTO 20 ! IF(X>0) GOTO 30 PROGRAM LOIF REAL x,y,z,G OPEN (8,FILE='Exp2in.txt') OPEN (7,FILE='Exp2Lout.txt') READ(8,10) X,Y,Z 10 FORMAT (3(2X,F6.2)) G=Z*Z-2.*X*Y WRITE(*,*) G IF(G <0) GOTO 20 IF(G==0) GOTO 30 IF(G >0) GOTO 40 20 WRITE(7,12) 12 FORMAT(5X,'g is negative') GOTO 50 30 WRITE(7,13) 13 FORMAT(5X,'no solution') GOTO 50 40 WRITE(7,14) 14 FORMAT(5X,'g is positive') READ(*,*) 50 STOP END PROGRAM LOIF

and Fortran95 program with if constructs:
!with if construct PROGRAM IFCONST REAL x,y,z,G OPEN (8,FILE='Exp2in.txt') OPEN (7,FILE='Exp2Cout.txt') READ(8,” (3(2X,F6.2))” ) X,Y,Z G=Z*Z-2.*X*Y WRITE(*,*) G if(G<0) then WRITE(7,”(5X,'g is negative')” ) else if(G==0) then WRITE(7, ”(5X,'no solution')” ) else WRITE(7, “(5X,'g is positive')” ) end if read(*,*) ! wait until reading the output on the display end IFCONST

2.5 Experimental Procedure

Write the activity programs for arithmetic logical and construct if into files “EXP2A.f90”, “EXP2L.f90”, “EXP2C.f90”. Debug and compile each of the source files in DOS command window with options –lst –noc . Prepare an input-data file Exp2in.txt that contains
0.5 12.1 -100 ↵

Execute the generated executables EXP2A.exe, EXP2L.exe and EXP2C.exe, and observe that the output files Exp2Aout.txt, Exp2Lout.txt, and Exp2Cout.txt are generated, and contains the correct output. At the end of the experimental procedure your work-folder shall contain the following files:
_command.bat,

CMPE 108 Algorithms and Programming, by Dr. M. Bodur,

Spring 08-09, Experiment #2

4

Exp2A.f90, Exp2L.f90, Exp2C.f90,

Exp2A.lst, Exp2A.exe, Exp2Ain.txt, Exp2Aout.txt, Exp2L.lst, Exp2L.exe, Exp2Lin.txt, Exp2Lout.txt, Exp2C.lst, Exp2C.exe, Exp2Cin.txt, Exp2Lout.txt,

Reporting After you complete the experimental procedure copy your work-folder into a flash or floppy disk and submit it to your assistant or instructor. Do not forget to sign the experiment-attendance, to delete your work-folder, and to turn off your computer before leaving out.

```
To top