Vous êtes sur la page 1sur 12

ChE 208 Computer Programming and Applications

Program Control

Why do we Need Program Control?

To Decide whether to execute some part of the program

October 9, 2011

To execute a specific part of the program

To repeat some part of the program

ChE 208

Lecture 4

Mohammad Mazharul Islam

2

IF-ELSE

An IF statement allows the programmer to execute a specific part of the program if certain condition (logical expression) is true.

program if certain condition (logical expression) is true. Mohammad Mazharul Islam IF (logical expression) THEN

Mohammad Mazharul Islam

IF (logical expression) THEN Statement sequence-1 ELSE Statement sequence-2 END IF October 9, 2011 ChE
IF
(logical expression) THEN
Statement sequence-1
ELSE
Statement sequence-2
END IF
October 9, 2011
ChE 208
Lecture 4

3

Solving Quadratic Equation using IF-ELSE Structure

PROGRAM QUAD1 REAL A, B, C, DISC, ROOT1, ROOT2 COMPLEX CROOT1, CROOT2

PRINT*,'ENTER THE COEFFICIENTS OF THE QUADRATIC EQUATION' READ*, A,B,C

DISC=B**2-4*A*C

Run it
Run it

IF(DISC<0) THEN DISC=SQRT(-DISC)

CROOT1=CMPLX(-B/(2*A),DISC/(2*A))

CROOT2=CONJG(CROOT1)

PRINT*,'THE COMPLEX ROOTS ARE',CROOT1, CROOT2

ELSE

DISC=SQRT(DISC)

ROOT1=(-B+DISC)/(2.0*A)

ROOT2=(-B-DISC)/(2.0*A)

PRINT*,'THE ROOTS ARE', ROOT1, ROOT2

END IF END PROGRAM QUAD1

October 9, 2011

ChE 208

Lecture 4

Mohammad Mazharul Islam

4

Nested IF

IF (logical-expression-1) THEN statement sequence-1 ELSE IF (logical-expression-2) THEN statement sequence-2 ELSE
IF
(logical-expression-1) THEN
statement sequence-1
ELSE
IF
(logical-expression-2) THEN
statement sequence-2
ELSE
statement sequence-3
END IF
END IF

October 9, 2011

ChE 208

Lecture 4

Mohammad Mazharul Islam

5

Nested IF

PROGRAM QUAD2 REAL A, B, C, DISC, ROOT1, ROOT2 COMPLEX CROOT1, CROOT2 PRINT*,'ENTER THE COEFFICIENTS OF THE QUADRATIC EQUATION' READ*, A,B,C

DISC=B**2-4*A*C

IF(DISC<0) THEN DISC=SQRT(-DISC)

CROOT1=CMPLX(-B/(2*A),DISC/(2*A))

CROOT2=CONJG(CROOT1)

PRINT*,'THE COMPLEX ROOTS ARE',CROOT1, CROOT2

ELSE

IF (DISC==0) THEN ROOT1 = -B/(2*A) PRINT*,’REPEATED ROOT IS’, ROOT1 ELSE

Run it
Run it

DISC=SQRT(DISC)

ROOT1=(-B+DISC)/(2.0*A)

ROOT2=(-B-DISC)/(2.0*A)

PRINT*,'THE ROOTS ARE', ROOT1, ROOT2

END IF

END IF END PROGRAM QUAD2

October 9, 2011

ChE 208

Lecture 4

Mohammad Mazharul Islam

6

October 9, 2011

IF-ELSE IF

IF (logical expression-1) THEN Statement sequence-1 ELSE IF (logical expression-2) THEN Statement sequence-2 ELSE IF
IF
(logical expression-1) THEN
Statement sequence-1
ELSE IF
(logical expression-2) THEN
Statement sequence-2
ELSE IF
(logical expression-3) THEN
Statement sequence-3
.
.
.
END IF
ChE 208
Lecture 4
Mohammad Mazharul Islam

7

IF-ELSE IF

PROGRAM QUAD3 REAL A, B, C, DISC, ROOT1, ROOT2 COMPLEX CROOT1, CROOT2

PRINT*,'ENTER THE COEFFICIENTS OF THE QUADRATIC EQUATION' READ*, A,B,C

DISC=B**2-4*A*C

IF(DISC<0) THEN DISC=SQRT(-DISC)

CROOT1=CMPLX(-B/(2*A),DISC/(2*A))

CROOT2=CONJG(CROOT1)

PRINT*,'THE COMPLEX ROOTS ARE',CROOT1, CROOT2 ELSE IF (DISC==0) THEN ROOT1 = -B/(2*A) PRINT*,’REPEATED ROOT IS’, ROOT1

ELSE

 

DISC=SQRT(DISC)

ROOT1=(-B+DISC)/(2.0*A)

ROOT2=(-B-DISC)/(2.0*A)

PRINT*,'THE ROOTS ARE', ROOT1, ROOT2

END IF

END

Run it
Run it

October 9, 2011

ChE 208

Lecture 4

Mohammad Mazharul Islam

8

Arithmetic IF

Arithmetic IF

statement causes transfer of control

depending on the value of an arithmetic expression.

Form of the statement:

October 9, 2011

IF

(arithmetic expression) s1, s2, s3 >0 <0 Arithmetic expression 0 GO TO s1 GO TO
(arithmetic expression) s1,
s2,
s3
>0
<0
Arithmetic
expression
0
GO TO s1
GO TO s2
GO TO s3

ChE 208

Lecture 4

Mohammad Mazharul Islam

9

Arithmetic IF

PROGRAM QUAD4 REAL A, B, C, DISC, ROOT1, ROOT2 COMPLEX CROOT1, CROOT2

PRINT*,'ENTER THE COEFFICIENTS OF THE QUADRATIC EQUATION' READ*, A,B,C

DISC=B**2-4*A*C

IF(B**2-4-A-C) 1,2,3

1 DISC=SQRT(-DISC)

CROOT1=CMPLX(-B/(2*A),DISC/(2*A))

CROOT2=CONJG(CROOT1)

PRINT*,'THE COMPLEX ROOTS ARE',CROOT1, CROOT2 STOP

2 ROOT1 = -B/(2*A) PRINT*,’REPEATED ROOT IS’, ROOT1 STOP

3 DISC=SQRT(DISC)

ROOT1=(-B+DISC)/(2.0*A)

ROOT2=(-B-DISC)/(2.0*A)

PRINT*,'THE ROOTS ARE', ROOT1, ROOT2 END PROGRAM QUAD4

October 9, 2011

ChE 208

Lecture 4

Mohammad Mazharul Islam

10

Task

Calculate the gas bill of your house from the 4-digit gas meter reading. The billing system is quite complex because of the following billing scheme:

CUBIC METERS. GAS USED

RATE

FIRST 70 m 3 NEXT 100 m 3 NEXT 230 m 3 ABOVE 400 m 3

Write a Fortran program to calculate the gas bill. The program should take current month’s reading and previous month’s reading as input and give gas bill as output.

BDT 700 BDT 7 PER m 3 BDT 3.5 PER m 3 BDT 2.1 PER m 3

October 9, 2011

ChE 208

Lecture 4

Mohammad Mazharul Islam

11

October 9, 2011

ChE 208

Lecture 4

Mohammad Mazharul Islam

12