Chapter 3 (Tutorial)
IF & CASE flow chart
PROGRAM if IF (consumed <250) THEN cost = consumed * 5 PROGRAM POWER_CONSUMPTION IMPLICIT NONE INTEGER :: consumed REAL :: cost WRITE (*,*) 'Enter consumed power (kWh) = ' READ (*,*) consumed IF (consumed <250) THEN cost = consumed * 5 ELSEIF (consumed <500) THEN cost = consumed * 10 ELSE cost = consumed * 20 END IF WRITE (*,*) 'Payment = RO ', cost/1000 END PROGRAM PROGRAM if
PROGRAM case PROGRAM POWER_CONSUMPTION IMPLICIT NONE INTEGER :: consumed REAL :: cost WRITE (*,*) 'Enter consumed power (kWh) = ' READ (*,*) consumed SELECT CASE (consumed) CASE (:249) cost = consumed * 5 CASE (250:499) cost = consumed * 10 CASE DEFAULT cost = consumed * 20 END SELECT WRITE (*,*) 'Payment = RO ', cost/1000 END PROGRAM PROGRAM case
If (logical_expr) GO TO 333 No Yes Go To 333
If (logical_expr) GO TO PROGRAM Password_Check IMPLICIT NONE CHARACTER (len = 8) :: password 333 WRITE (*,*) ‘ENTER PASSWORD’ write (*,*) READ (*,*) password If (password /= ‘fortran’) GO TO 333 WRITE (*,*) “ OK, Password correct.” END PROGRAM
Integrate all in one
Integrate all in one PROGRAM Protected_Power IMPLICIT NONE CHARACTER (len = 8) :: password INTEGER :: consumed REAL :: cost 333 WRITE (*,*) 'ENTER PASSWORD' write (*,*) READ (*,*) password If (password /= 'fortran') GO TO 333 WRITE (*,*) 'Enter consumed power (kWh) = ' READ (*,*) consumed SELECT CASE (consumed) CASE (:249) cost = consumed * 5 CASE (250:499) cost = consumed * 10 CASE DEFAULT cost = consumed * 20 END SELECT WRITE (*,*) 'Payment = RO ', cost/1000 END PROGRAM
One more trick Make a non-terminated program that is always waiting for consumed power to calculate cost.
One more trick PROGRAM nonTerm_Power IMPLICIT NONE CHARACTER (len = 8) :: password INTEGER :: consumed REAL :: cost 333 WRITE (*,*) 'ENTER PASSWORD' write (*,*) READ (*,*) password If (password /= 'fortran') GO TO 333 444 WRITE (*,*) 'Enter consumed power (kWh) = ' READ (*,*) consumed SELECT CASE (consumed) CASE (:249) cost = consumed * 5 CASE (250:499) cost = consumed * 10 CASE DEFAULT cost = consumed * 20 END SELECT WRITE (*,*) 'Payment = RO ', cost/1000 GO TO 444 END PROGRAM
Practical Exercise Design a FORTRAN program for the following problem and run it on computer. A student need to calculate the GPA of a semester that consists of six courses. The user should enter course number, course credits and grade letter for each course. The ouput should be the overall semester GPA and total number of credits earned.