You are on page 1of 9

EET110 Basic Computer Programming Laboratory Module

School of Electrical System Engineering


Universiti Malaysia Perlis

Name :..
Matrix No:..
Year :
Course :...

LAB 3

SELECTION STRUCTURE

School of Electrical System Engineering Sem 2 (2016/2017)


1
EET110 Basic Computer Programming 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; start
statement2;

}
condition Yes statement1

No

2.2 Two way selection statement


The syntax for two way selection is as below:
if (condition) if (condition)
statement; or {
else statement1;
statement; statement2;
}
else
{
statement1;
statement2;
}

School of Electrical System Engineering Sem 2 (2016/2017)


2
EET110 Basic Computer Programming Laboratory Module

Flowchart:

start

statement2 No condition Yes statement1

2.3 Multi selection statement


The syntax for multi selection statement is as below:
Flowchart:

if (condition) start
statement;
else if (condition)
statement; condition T statement1
else
statement; F

condition T statement1

statement1

end

2.4 Nested if statement


Nested if statement is when one if statement is placed inside another if statement.
The nested if statement is used to code decisions with multiple alternatives. The
nested if selection can be modified with the use of logic operator (AND) to combine
two or more conditional (relation) statements to be as one condition statement, thus
omits the nested if statement.

Nested if Use of logic operator

if (condition) if ((condition) && (condition))


if (condition) statement;
statement; else
else if (condition) statement;
statement;
else
statement;

School of Electrical System Engineering Sem 2 (2016/2017)


3
EET110 Basic Computer Programming Laboratory Module

Flowchart:
start

condition T
F

condition

statement1
statement1 statement2

statement1

end

2.5 Switch statement


The switch statement is another way to do multi selections statement. The syntax is
as follow:

switch (variable)
{
case value1: printf(Message 1);
break;
case value2: printf(Message 2);
break;
case value3: printf(Message 3);
break;
case value4: printf(Message 4);
break;
case value5: printf(Message 5);
break;
default: printf(Default Message);
}

Note: The break statement is used to end the switch after the category or selection is selected.
Omitting the break statement will not generate a syntax error in your program but it will produce
semantic error.

Question: Find out what are syntax and semantic errors.

School of Electrical System Engineering Sem 2 (2016/2017)


4
EET110 Basic Computer Programming 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");

(b) Rewrite the following if statement as an equivalent switch statement. The


variable digit is of type int.
if (iDigit == 0)
iValue = 3;
else if (iDigit == 1)
iValue = 3;
else if (iDigit == 2)
iValue = 6;
else if (iDigit == 3)
iValue = 9;

School of Electrical System Engineering Sem 2 (2016/2017)


5
EET110 Basic Computer Programming 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) Fine ( $)


65 or less 0
66-70 15.00
71-75 30.00
76-80 75.00
over 80 100.00

(d) Write the output for the following code segment.


v1 = 15.0;
v2 = 0.5;
if (v1 > 10.0)
printf("ten ");
else if (v1 > 14.0)
printf("fourteen ");
if (v2 * v1 > 7.0)
printf("seven ");
if (v1 - v2 > 9.0)
printf("nine ");
printf("\n");

School of Electrical System Engineering Sem 2 (2016/2017)


6
EET110 Basic Computer Programming Laboratory Module

3.2 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).

School of Electrical System Engineering Sem 2 (2016/2017)


7
EET110 Basic Computer Programming Laboratory Module

3.3 Write a nested if statement for the decision diagrammed in the accompanying
flowchart. Use a multiple-alternative if for intermediate decisions where possible.

false true
pH > 7

true pH < 12 true


pH == 7

Neutral
false false

true Alkaline
pH > 2

false Very Alkaline

Very Acidic Acidic

School of Electrical System Engineering Sem 2 (2016/2017)


8
EET110 Basic Computer Programming Laboratory Module

3.4 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) Brightness (Lumens)


15 125
25 215
40 500
60 880
75 1000
100 1675

Assign -1 to lumens if the value of watts is not in the


table.

a. Write down the flowchart for the program.

b. Write a switch statement based on flowchart in (a).

c. Write a nested if statement equivalent to the switch statement (b) based


on flowchart in (a).

School of Electrical System Engineering Sem 2 (2016/2017)

You might also like