Download presentation
Presentation is loading. Please wait.
1
Chapter 3 (Tutorial)
2
IF & CASE flow chart
3
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
4
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
5
If (logical_expr) GO TO
333 No Yes Go To 333
6
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
7
Integrate all in one
8
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
9
One more trick Make a non-terminated program that is always waiting for consumed power to calculate cost.
10
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
11
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.