Documente Academic
Documente Profesional
Documente Cultură
Laboratory Module
LAB 4
SELECTION STRUCTURES
Laboratory Module
1. OBJECTIVES:
1.1 Introduction to if, if . else, nested if and switch statements.
1.2 Able to use selection control structures in programs.
1.3 Able to modify program according to user requirements.
2 INTRODUCTION:
In C language, there are several selection methods which can be used:
(a) One way selection
(b) Two way selection
(c) Multi selection
(d) Nested if
(e) Switch command
2.1 One way selection statement
The syntax for one way selection is as below:
if (condition)
statement;
Compound statement: a group of statements bracketed by { and } that are
executed sequentially. { (open curly bracket) and } (close curly bracket) must
be used to show the start and end of the commands under the if statement.
if (condition)
{
Flowchart:
statement1;
statement2;
start
}
condition
Yes
statement1
No
or
if (condition)
{
statement1;
statement2;
}
else
{
statement1;
statement2;
}
Laboratory Module
Flowchart:
start
statement2
No
condition
Yes
statement1
start
condition
statement1
F
condition
statement1
F
statement1
end
if (condition)
if (condition)
statement;
else if (condition)
statement;
else
statement;
Laboratory Module
Flowchart:
start
condition
condition
statement1
statement1
statement2
statement1
end
Laboratory Module
3 TASKS:
3.1
(a) The following code segment is syntactically correct, but difficult to read. Rewrite
the segment using indentation that improves its readability.
if (cRoadStat == 's')
if (fTemp > 0)
printf("Roads wet.\n");
else
printf("Roads icy.\n");
else
printf("Roads dry.\n");
Laboratory Module
(c) The decision table below shows fines imposed for speeding violations. Write a
code segment that assigns the correct fine to type double variable fine based on
the value of type int variable speed.
Speed (mph)
65 or less
66-70
71-75
76-80
over 80
Fine ($)
0
15.00
30.00
75.00
100.00
(d) Evaluate the expression below assuming a is 5, flag is 1, and c is 15. What part
of the expression is not computed at all because of short-circuit evaluation?
a != 3 && flag || c >= 10
Laboratory Module
3.2 Write an interactive program that contains an if statement that may be used to
compute the area of square (area = side2) or a triangle (area = x base x height )
after prompting the user to type the first character of the figure names (S or T).
a. Write down the flowchart for the program.
b. Write your program based on flowchart in (a).
Laboratory Module
3.3 Write a program for the National Earthquake Information centre 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.5
Serious damage: wall may crack or fall
6.5 N 7.5
Disaster: house and buildings may collapse
higher
Catastrophe: most buildings destroyed
a. Write down the flowchart for the program.
b. Can you handle this problem with a switch statement? If so, use a switch
statement; if not, explain why;
c. Write a program based on flowchart in (a).
Laboratory Module
3.4 Write a nested if statement for the decision diagrammed in the accompanying
flowchart. Use a multiple-alternative if for intermediate decisions where possible.
false
pH == 7
Neutral
true
false
Alkaline
true
Very Alkaline
false
Very Acidic
true
pH < 12
true
false
pH > 2
pH > 7
Acidic
Laboratory Module
3.5 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.
QII
QI
x
QIII
QIV
10
Laboratory Module
3.6 Write a program that assigns to the variable lumens the expected brightness of a
standard light bulb whose power has been stored in watts. Use the following table:
Power (Watts)
15
25
40
60
75
100
Brightness (Lumens)
125
215
500
880
1000
1675
11