lab-04 if structure i putu danu raharja raharja @kfupm.edu.sa information & computer science...

12
LAB-04 IF Structure I Putu Danu Raharja raharja @kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals

Upload: garey-patrick

Post on 18-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: LAB-04 IF Structure I Putu Danu Raharja raharja @kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals

LAB-04IF Structure

I Putu Danu Raharjaraharja @kfupm.edu.sa

Information & Computer Science DepartmentCCSE - King Fahd University of Petroleum & Minerals

Page 2: LAB-04 IF Structure I Putu Danu Raharja raharja @kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals

ICS101-LAB04-Term063 2

IF Structure

What is the use of IF structure? What is the type of expression we can use for

condition? Can we write multiple statements in an IF

construct?

Page 3: LAB-04 IF Structure I Putu Danu Raharja raharja @kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals

ICS101-LAB04-Term063 3

IF Constructs (1): IF (pp. 37-38)

IF (condition) THEN

STATEMENT_BLOCK

ENDIF

When is STATEMENT_BLOCK executed? What if condition is .FALSE.?

Page 4: LAB-04 IF Structure I Putu Danu Raharja raharja @kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals

ICS101-LAB04-Term063 4

IF Construct(1)

YES

Is Condition .T

RUE. ?

STATEMENT_BLOCK

NO

Page 5: LAB-04 IF Structure I Putu Danu Raharja raharja @kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals

ICS101-LAB04-Term063 5

IF Constructs (2): IF-ELSE (pp. 35-36)

IF (condition) THEN

STATEMENT_BLOCK1

ELSE

STATEMENT_BLOCK2

ENDIF

When is STATEMENT_BLOCK1 executed? What about STATEMENT_BLOCK2?

Page 6: LAB-04 IF Structure I Putu Danu Raharja raharja @kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals

ICS101-LAB04-Term063 6

IF Constructs (2): IF-ELSE (pp. 35-36)

YESIs Condition .T

RUE. ?

STATEMENT_BLOCK2

NO

STATEMENT_BLOCK1

Page 7: LAB-04 IF Structure I Putu Danu Raharja raharja @kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals

ICS101-LAB04-Term063 7

IF Constructs (3): IF-ELSEIF (pp.38-42)

IF (condition_1) THENSTATEMENT_BLOCK_1

ELSEIF (condition_2) THENSTATEMENT_BLOCK_2

…ELSEIF (condition_m) THENSTATEMENT_BLOCK_M

ELSESTATEMENT_BLOCK_N

ENDIF When is STATEMENT_BLOCK1 executed? What about STATEMENT_BLOCK2? What about STATEMENT_BLOCK_N?

Page 8: LAB-04 IF Structure I Putu Danu Raharja raharja @kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals

ICS101-LAB04-Term063 8

IF Constructs (3): IF-ELSEIF (pp.38-42)

YESCondition1 .TRU

E. ?

NO

STATEMENT BLOCK1YES

Condition2 .TRU

E. ?

NO

STATEMENT BLOCK2YES

Condition3 .TRU

E. ?

NO

STATEMENT BLOCK3

Page 9: LAB-04 IF Structure I Putu Danu Raharja raharja @kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals

ICS101-LAB04-Term063 9

IF Constructs (4): Simple IF (pp.42-44)

IF (condition) statement

The same as regular IF construct but it can only execute one statement

When is statement executed?

Page 10: LAB-04 IF Structure I Putu Danu Raharja raharja @kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals

ICS101-LAB04-Term063 10

Exercise-1

Implement the following flow diagram using a nested IF-ELSEIF structure.

Age > 59

Age > 20

Age > 12

“ADULT”

“TEEN”

“CHILD”

Sts is ‘W’

“Working Senior”

“Retired Senior”

Page 11: LAB-04 IF Structure I Putu Danu Raharja raharja @kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals

ICS101-LAB04-Term063 11

Exercise-2

The National Earthquake Information Center has asked you to write a program implementing the following decision table to

characterize an earthquake based on its Richter scale number.

Richter Scale Number (N) Characterization

N < 5.0 Little or no damage

5.0 <= N < 5.5 Some damage

5.5 <= N < 6.5Serious damage; wall may crack or fall.

6.5 <= N < 7.5Disaster; houses and buildings may collapse

higher Catastrophe; most building destroyed.

Page 12: LAB-04 IF Structure I Putu Danu Raharja raharja @kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals

ICS101-LAB04-Term063 12

Exercise-3

Write a program that takes the X-Y coordinates of a point in the Cartesian plane and displays a message telling either an axis on which the point lies or the quadrant in which it is found.

Y

X

Q-1Q-2

Q-3 Q-4

Sample lines of output:(-1.0 , -2.5) is in quadrant III(0.0 , 4.8) is on the Y axis(5.6 , 0.0) is on the X axis(0.0 , 0.0) is on the center.