LAB04 IF Structure I Putu Danu Raharja raharja
LAB-04 IF Structure I Putu Danu Raharja raharja @kfupm. edu. sa Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals ICS 101 -LAB 04 -Term 073
IF Structure n What is the use of IF structure? n What is the type of expression we can use for condition? n Can we write multiple statements in an IF construct? ICS 101 -LAB 04 -Term 073 2
IF Constructs (1): IF (pp. 37 -38) IF (condition) THEN STATEMENT_BLOCK ENDIF n When is STATEMENT_BLOCK executed? n What if condition is. FALSE. ? ICS 101 -LAB 04 -Term 073 3
IF Construct(1) Is Condition. TRUE. ? NO YES STATEMENT_BLOCK ICS 101 -LAB 04 -Term 073 4
IF Constructs (2): IF-ELSE (pp. 35 -36) IF (condition) THEN STATEMENT_BLOCK 1 ELSE STATEMENT_BLOCK 2 ENDIF n When is STATEMENT_BLOCK 1 executed? n What about STATEMENT_BLOCK 2? ICS 101 -LAB 04 -Term 073 5
IF Constructs (2): IF-ELSE (pp. 35 -36) NO STATEMENT_BLOCK 2 ICS 101 -LAB 04 -Term 073 Is Condition. TRUE. ? YES STATEMENT_BLOCK 1 6
IF Constructs (3): IF-ELSEIF (pp. 38 -42) IF (condition_1) THEN STATEMENT_BLOCK_1 ELSEIF (condition_2) THEN STATEMENT_BLOCK_2 … ELSEIF (condition_m) THEN STATEMENT_BLOCK_M ELSE STATEMENT_BLOCK_N ENDIF n When is STATEMENT_BLOCK 1 executed? n What about STATEMENT_BLOCK 2? n What about STATEMENT_BLOCK_N? ICS 101 -LAB 04 -Term 073 7
IF Constructs (3): IF-ELSEIF (pp. 38 -42) NO NO NO Condition 3. TRUE. ? Condition 2. TRUE. ? YES Condition 1. TRUE. ? YES STATEMENT BLOCK 1 STATEMENT BLOCK 2 STATEMENT BLOCK 3 ICS 101 -LAB 04 -Term 073 8
IF Constructs (4): Simple IF (pp. 42 -44) IF (condition) statement n The same as regular IF construct but it can only execute one statement n When is statement executed? ICS 101 -LAB 04 -Term 073 9
Exercise-1 n Implement the following flow diagram using a nested IF-ELSEIF structure. ICS 101 -LAB 04 -Term 073 10
Exercise-2 n 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) N < 5. 0 Characterization Little or no damage 5. 0 <= N < 5. 5 Some damage 5. 5 <= N < 6. 5 Serious damage; wall may crack or fall. 6. 5 <= N < 7. 5 Disaster; houses and buildings may collapse higher ICS 101 -LAB 04 -Term 073 Catastrophe; most building destroyed. 11
Exercise-3 n 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. Q-2 Q-3 Y Q-1 Q-4 X 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. ICS 101 -LAB 04 -Term 073 12
Exercise-4 n Write a program that takes three integers and then prints them in an ascending order. Sample lines of output: Enter 3 integers: 100 -7 -11 -7 100 ICS 101 -LAB 04 -Term 073 13
- Slides: 13