Professional Documents
Culture Documents
CO1003 - Chapter 4 - Selection Statements
CO1003 - Chapter 4 - Selection Statements
CO1003 - Chapter 4 - Selection Statements
3
Content
p Introduction
p if.. statements
p if..else.. statements
p Nested if../if..else.. statements
p switch..case.. statements
p Summary
4
Introduction
p Recall
n Statement
p ended with a semicolon (;)
p stretched on multiple lines with a backslash \ at the end
p able to be grouped in the brackets {}
p not consider spaces
n Block
p specified by {} with no semicolon after the right brace
p contains as many statements as required
p is a compound statement, syntactically equivalent to a
single statement
à Sequentially processed from the beginning to the
end of a function 5
Introduction
p Given a void main() {
double positiveNumber[10] = {2, 1, 3, 10, 8, 3, 4, 5, 9, 12};
set of n
int n = 10;
positive double minNumber = positiveNumber[0];
int iteration = 1;
numbers,
while (iteration < n) {
find the if (minNumber <= positiveNumber[iteration])
Single
iteration = iteration + 1; statement
smallest
else {
one. minNumber = positiveNumber[iteration];
Block
iteration = iteration + 1;
(Chapter 1 –
}
Real code in C) }
}
6
Introduction
p Control statements in C
n Sequence
p Assignment
p Function calling
p …
n Selection
p if
p if..else..
p switch..case..
n Repetition
p for..
p while..
p do..while..
7
Introduction
p Given a void main() {
double positiveNumber[10] = {2, 1, 3, 10, 8, 3, 4, 5, 9, 12};
set of n
int n = 10;
positive double minNumber = positiveNumber[0];
int iteration = 1;
numbers,
while (iteration < n) {
find the if (minNumber <= positiveNumber[iteration])
iteration = iteration + 1;
smallest
else {
one. minNumber = positiveNumber[iteration];
iteration = iteration + 1;
(Chapter 1 –
} Control Statements for Selection
Real code in C) }
}
8
if.. statements
if (<Condition>) <Statement>
false (0)
<Condition>
if (<Condition> )
{ true ( 0)
<Statements>
}
<Statements>
false (0)
grade>=5.0
true ( 0)
printf(“Passed”);
if (<Condition> )
{
<Statements T>
}
else <Statements T> is performed (selected)
{
<Statements F> if <Condition> is true.
} Otherwise, <Statements F> is performed.11
if..else.. statements
false (0)
grade >= 5.0
true ( 0)
printf(“Passed”); printf(“Failed”);
13
if..else.. statements
Which one do you prefer:
conditional expressions or
if..else.. statements?
14
Nested if../if..else.. statements
if (<condition 1>) if (<condition 1>)
{ {
… …
if (<condition 2>) … if (<condition 2>) …
… …
} }
else
{
…
if (<condition 3>) …
…
}
15
Nested if../if..else.. statements
16
Nested if../if..else.. statements
17
Nested if../if..else.. statements
A multi-way decision if (<condition 1>) <statements T1>
else if (<condition 2>) <statements T2>
else if (<condition 3>) <statements T3>
…
else if (<condition k>) <statements Tk>
else <statements Fk>
18
Nested if../if..else.. statements
Be careful with specifying “else” for “which if”:
if (<condition 1>)
if (<condition 2>) <statements T2>
else <statements F>
should be:
if (<condition 1>) {
d = ? 5? 10? 20?
if (<condition 2>) <statements T2>
}
else <statements F1>
or:
if (<condition 1>) {
if (<condition 2>) <statements T2>
else <statements F2>
d = ? 5? 10? 20?
19
}
switch..case.. statements
false (0) false (0) false (0)
<case 1> <case 2> <case N>
true ( 0) true ( 0) true ( 0)
23
switch..case.. statements
false (0) false (0) false (0)
<case 1> <case 2> <case N>
true ( 0) true ( 0) true ( 0)
switch (<expression>) {
case <case 1>: <Statements 1>
case <case 2>: <Statements 2>
…
case <case N>: <Statements N>
[default: <Default>]
} 24
switch..case.. statements
switch..case.. statement with “fall-through” property
can be regarded as:
if (<expression> == <case 1>) {
switch (<expression>) { <Statements 1>
case <case 1>: <Statements 1> <Statements 2>
case <case 2>: <Statements 2> …
… <Default>
case <case N>: <Statements N> }
[default: <Default>] else if (<expression> == <case 2>) {
} <Statements 2>
…
<Default>
}
…
else if (<expression> == <case N>) {
<Statements N>
<Default>
}
25
[else <Default>]
switch..case.. statements
false (0) false (0) false (0)
aChar==‘a’ aChar==‘b’ …
true ( 0) true ( 0) true ( 0)
printf
printf ‘a’; printf ‘b’; …
default;
26
27
28
Put them all together
p Given a problem: build your timetable in a
week. Input a day in a week and output its
corresponding activities.
p string.h: a standard library file for strings
n Compare two strings
p int strcmp(const char *str1, const char *str2)
§ < 0 if str1 < str2 (less than)
§ > 0 if str1 > str2 (greater than)
§ = 0 if str1 = str2 (equal)
30
Summary
p Control statements for selection
n if.. statements
n if..else.. statements
n switch..case.. statements
32