Computers in Engineering - 308-208 Lecture 6
Computers in Engineering - 308-208

# Lesson 6 - Learning Goals

## 6.3 Variations of the IF-THEN-ELSE Statement

THE ELF90 LANGUAGE

Philosophy of ELF90 - A More Disciplined FORTRAN

Enforce structured, Fortran-90-standard coding. Remove redundant features. Keep all the power of the Fortran language.

STRUCTURED CODING PRACTICE ENFORCED BY ELF90

CONSTRUCTS FOR STRUCTURED PROGRAMMING

The four main constructs used in structured programming are:-

1. Sequence of operations

2. Decision making
3. Loop with a test at the beginning
4. Loop with a test at the end

VARIATIONS OF THE IF-THEN-ELSE

1) Omitting ELSE block :

IF ( logical expression ) THEN
statements
.
.
.
END IF

2) Only one instruction to execute IF condition is met :

IF ( logical expression ) statement

3) Need IF-THEN-ELSE structure :

4) Need to imbed several IF-THEN-ELSE structures :

IF ( logical expression ) THEN
statements
.
.
ELSE IF ( logical expression ) THEN
statements
.
.
ELSE
statements
.
.
END IF

```!
PROGRAM P33
!
! Simple loop with IF-THEN-ELSE
!
IMPLICIT NONE
INTEGER :: I,J
REAL :: SUM,COUNT,AVE
!
PRINT *, 'This is Program >>P33 - IF-THEN-ELSE'
!
!
!
DO J=1,I
IF(COUNT == 0.0) THEN
PRINT *,'COUNT ZERO'
STOP
ELSE
AVE=SUM/COUNT
PRINT *,'SUM=', SUM
PRINT *,'COUNT=', COUNT
PRINT *,'AVERAGE=', AVE
END IF
END DO
STOP
END PROGRAM P33
```

```    PROGRAM P36
!
! Nested IF blocks
!
IMPLICIT NONE
LOGICAL :: SINGLE
CHARACTER (LEN=20) :: NAME
INTEGER :: I,J,AGE,N,TAGE
REAL :: AVE
!
!
PRINT *, 'This is Program >> P36 - Nested IF blocks'
!
!
!
N=0
TAGE=0.0
L1: DO J=1,I
PRINT * ,NAME,SINGLE,AGE
IF(SINGLE)THEN
N=N+1
TAGE=TAGE+AGE
IF(N == 5)THEN
AVE=TAGE/5.0
PRINT * ,'AVERAGE AGE OF FIRST FIVE SINGLES IS'
PRINT * ,AVE
STOP
END IF
END IF
END DO L1
STOP
END PROGRAM P36
```

```PROGRAM P37
!
! DECLARATIONS
!
IMPLICIT NONE
INTEGER :: AGE,WEIGHT,MARRGS
CHARACTER (LEN=15) :: GROUP,NAME
LOGICAL :: SINGLE,HEAVY
!
PRINT *, 'This is Program >>P37 - Classification program'
!
! Tell program where data for READ * is coming from
OPEN(UNIT=5, FILE='P37.DAT') ! UNIT=5 is the default input
!
! MAIN STATEMENTS
!
PRINT *,'CLASSIFICATION PROGRAM'
!
Loop1: DO
PRINT * ,' '
!
IF(AGE == 99)STOP ! Yes == for testing in an IF
HEAVY = .FALSE. ! One = for an assignment statement
IF(WEIGHT > 70) HEAVY=.TRUE.
!
IF(AGE < 18)THEN
GROUP='MINOR'
ELSE IF(AGE < 35)THEN
GROUP='PRIME YEARS'
ELSE
GROUP='MATURE'
END IF
!
!
IF(SINGLE)THEN
PRINT * ,NAME,' IS SINGLE'
ELSE
PRINT * ,NAME,'WAS MARRIED',&
MARRGS,'TIMES'
END IF
!
PRINT * ,'SHE IS',AGE,'YEARS OLD'
PRINT * ,'AND WEIGHS',WEIGHT,'KILOGRAMS'
IF(HEAVY)PRINT * , &
'AND SHOULD JOIN WEIGHT WATCHERS'
PRINT * ,'AGE GROUP: ',GROUP
END DO Loop1
STOP
END PROGRAM P37
```

```PROGRAM P38
!
! This program will find the perimeter and the area of a triangle
!
IMPLICIT NONE
REAL :: A,B,C,PERI,SEMI,AREA
INTEGER :: N
!
! Tell program where data for READ * is coming from
OPEN(UNIT=5, FILE='P38.DAT')
! UNIT=5 is the default input
!
!
DO N=1,20
IF( A == -90.0 ) STOP
IF( A <= 0 .OR. B <= 0 .OR. C <= 0 ) THEN
PRINT 5,A,B,C
PRINT *,'ALL THE VALUES MUST BE POSITIVE.'
5   FORMAT(' A=',F8.3,' B=',F8.3,' C=',F8.3)
ELSE
PERI = A+B+C
SEMI = PERI/2
AREA = SEMI * (SEMI-A) * (SEMI-B) * (SEMI-C)
IF (AREA .LT. 0) THEN
PRINT 5,A,B,C
PRINT *,'TWO SIDES SHORTER THAN THIRD'
ELSE
PRINT 8,A,B,C,PERI,SQRT(AREA)
8     FORMAT(' A=',F8.3,' B=',F8.3,' C=',F8.3,' PERIMETER =', &
F8.3,' AREA=',F8.3)
END IF
END IF
END DO
STOP
END PROGRAM P38
```

```PROGRAM GRADE
!
IMPLICIT NONE
REAL :: FINAL
CHARACTER (LEN=20) :: NAME
!
.
.
.
IF(FINAL > 85.0) THEN
ELSE IF (FINAL > 80.0) THEN
ELSE IF (FINAL > 75.0) THEN
ELSE IF (FINAL > 70.0) THEN
.
.
.
ELSE IF (FINAL > 50.0) THEN