lab-04 if structure i putu danu raharja raharja @kfupm.edu.sa information & computer science...
Post on 18-Dec-2015
215 Views
Preview:
TRANSCRIPT
LAB-04IF Structure
I Putu Danu Raharjaraharja @kfupm.edu.sa
Information & Computer Science DepartmentCCSE - 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?
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.?
ICS101-LAB04-Term063 4
IF Construct(1)
YES
Is Condition .T
RUE. ?
STATEMENT_BLOCK
NO
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?
ICS101-LAB04-Term063 6
IF Constructs (2): IF-ELSE (pp. 35-36)
YESIs Condition .T
RUE. ?
STATEMENT_BLOCK2
NO
STATEMENT_BLOCK1
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?
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
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?
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”
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.
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.
top related