Professional Documents
Culture Documents
Selection
(Part 2)
Knowledge:
Understand various concepts of selection control structure
Skill:
Be able to develop a program containing selection control structure
FTSM
if Statements
if else - if
if
if - else
TK1913-C Programming
if Statement
The structure is similar to single selection (flowchart)
Syntax:
Dont forget the
if (expression)
brackets !!
statement;
or
if (expression) {
Dont forget the
statement1;
curly brackets !!
statement2;
}
TK1913-C Programming
if Statement
The similarity between single selection structure and
if statement:
Single Selection:
if <condition is true> start
step 1
step 2
step k
end_if
TK1913-C Programming
if Statement:
if (<condition>) {
statement 1
statement 2
statement k
}
4
if Statement
Example:
int num1, num2, min;
printf(Key-in 2 numbers: );
20
scanf(%d%d, &num1, &num2);
min = num1;
if (num1 > num2)
min = num2;
printf(Smallest: %d\n, min);
> 15?
num1
?
20
num2
15
?
min
15
20
?
Key-in 2 numbers: 20
_ 15
_Smallest: 15
_
TK1913-C Programming
if Statement
Example:
int mark;
92 > 80?
printf(Mark: );
scanf(%d, &mark);
if (mark > 80) {
printf(Category: Excellent\n);
printf(Congratulations!);
}
mark
?
92
Mark: 92
_
_
Category:
Excellent
Congratulations!
TK1913-C Programming
if Statement
Example:
void main() {
int mark;
printf(Mark: );
scanf(%d, &mark);
if (mark >= 50)
printf(Pass\n);
printf(Your mark is %d, mark);
}
TK1913-C Programming
if Statement
Example:
void main() {
int mark;
printf(Mark: );
scanf(%d, &mark);
if (mark >= 50)
printf(Pass\n);
printf(Your mark is %d, mark);
}
TK1913-C Programming
if - else Statement
The structure is similar to double selection (flowchart)
Syntax:
or
if (expression)
statement;
else
statement;
if (expression) {
statement1;
statement2;
} else
statement3;
TK1913-C Programming
if - else Statement
or
if (expression) {
statement1;
statement2;
} else {
statement3;
statement4;
}
TK1913-C Programming
10
if else Statement
The similarity between double selection structure
and if - else statement:
Double Selection:
if <condition is true> start
step 1
step k
end_if
else start
step 1
step n
end_else
TK1913-C Programming
if Statement:
if <condition> {
statement 1
statement k
}
else {
statement 1
statement n
}
11
if - else Statement
Example:
if (num1 < num2)
10 < 15?
min = num1;
else
min = num2;
printf(Smallest: %d\n, min);
num1
10
num2
15
min
10
?
_Smallest: 10
_
TK1913-C Programming
12
if - else Statement
Example:
if (num1 < num2)
20 < 15?
min = num1;
else
min = num2;
printf(Smallest: %d\n, min);
num1
20
num2
15
min
15
?
_Smallest: 15
_
TK1913-C Programming
13
if - else Statement
Example:
num1 700
14
if else Statement
Example:
void main() {
int mark;
printf(Mark: );
scanf(%d, &mark); What will the output
be if the mark is 74?
if (mark >= 50)
printf(Pass\n);
else
printf(Fail\n);
printf(Your mark is %d, mark);
}
TK1913-C Programming
15
if else Statement
Example:
void main() {
int mark;
printf(Mark: );
scanf(%d, &mark); What will the output
if (mark >= 50)
be if the mark is 14?
printf(Pass\n);
else
printf(Fail\n);
printf(Your mark is %d, mark);
}
TK1913-C Programming
16
if else Statement
Example:
What will the output
void main() {
be if the mark is 14?
int mark;
printf(Mark: );
scanf(%d, &mark); What will the output
if (mark >= 50)
be if the mark is 70?
printf(Pass\n);
else {
printf(Fail\n);
printf(Your mark is %d, mark);
}
}
TK1913-C Programming
17
Take a break .
Lets have a minute break.
While youre having a break, let
me introduce you some program
styles..
Why we need program
styles?.To ensure your
program is readable.
Lets look some
examples..
TK1913-C Programming
18
19
Difficult to read!!!
void main() {
int mark;
Dont you think so??
printf(Mark: );
scanf(%d, &mark);
if (mark >= 50)
printf(Pass\n);
else
printf(Fail\n);
printf(Your mark is %d, mark);
}
TK1913-C Programming
20
Lets recap
Syntax:
if (expression)
statement;
else
statement;
if-else
if statement
statement
21
if else - if Statement
Syntax:
if (expression)
statement;
else if (expression)
statement;
else if (expression)
statement;
else
statement;
TK1913-C Programming
if-else-if statement
22
if else - if Statement
Syntax:
if (expression)
statement;
else if (expression)
statement;
else if (expression)
statement;
else (expression)
statement;
TK1913-C Programming
if-else-if statement
Be carefulcommon
mistake made by
students !!
23
Lets recap
Example:
Multiple Selection
step
Assume
m, step
condition
. will1 be
is
end_if
true,
executed
so
if <condition_2 is true> start
step n
end_if
else start
step x
end_else
TK1913-C Programming
24
Lets recap
Example:
Multiple Selection
step
Assume
m, step
condition
. will1be
is
end_if
skipped,
false,
andso
if <condition_2 is true> start
step n
condition 2 will be tested
end_if
else start
step x
end_else
TK1913-C Programming
25
Lets recap
Example:
Multiple Selection
end_if
if <condition_2 is true> start
step n
step
Assume
n, step
condition
. will2be
is
end_if
true,
executed
so
else start
step x
end_else
TK1913-C Programming
26
Lets recap
Example:
Multiple Selection
end_if
if <condition_2 is true> start
step n
Assume
step n, condition
step . will
2 isbealso
end_if
skipped,
andso
false,
else start
step x
step x will be executed
end_else
TK1913-C Programming
27
Multiple Selection in C
Example:
#include <stdio.h>
int main( ) {
char letter;
scanf(%c, &letter);
Is the letter an upper case?
if (letter >= a && letter <= z )
printf(Lower case\n);
else if (letter >= A && letter <= Z)
Is the letter a digit?
printf(Upper case\n);
else if (letter >= 0 && letter <= 9)
printf(Digit\n);
else
printf(Special character\n);
}
TK1913-C Programming
28
Multiple Selection in C
Example:
#include <stdio.h>
int main( ) {
char letter;
29
Exercise
Develop a program for the following problem.
Given a mark, determine its grade based on the
table below:
74 < mark < 100
64 < mark < 75
54 < mark < 65
39 < mark < 55
0 < mark < 40
others
TK1913-C Programming
grade = A
grade = B
grade = C
grade = D
grade = E
error message
30
A
n
s
w
e
r
1
int mark;
printf(Key-in the mark: );
scanf(%d,&mark);
if ((mark >= 75) && (mark <= 100))
printf("Grade = A);
else if ((mark >= 65) && (mark <= 74))
printf(" Grade = B);
else if ((mark >= 55) && (mark <= 64))
printf(" Grade = C);
else if ((mark >= 40) && (mark <= 54))
printf(" Grade = D);
else if ((mark >= 0) && (mark <= 39))
printf(" Grade = E);
else
printf(Input error\n);
TK1913-C Programming
31
A
n
s
w
e
r
2
int mark;
char grade ;
printf(Key-in the mark : );
scanf(%d,&mark);
if ((mark >= 75) && (mark <= 100))
grade =A;
else if ((mark >= 65) && (mark <= 74))
grade =B;
else if ((mark >= 55) && (mark <= 64))
grade =C;
else if ((mark >= 40) && (mark <= 54))
grade =D;
else if ((mark >= 0) && (mark <= 39))
grade =E;
else
printf(Input error\n);
printf(Your grade is %c, grade );
TK1913-C Programming
32
A
n
s
w
e
r
3
int mark;
char grade;
printf(Key-in the mark: );
scanf(%d,&mark);
if ((mark >= 75) && (mark <= 100))
grade=A;
else if ((mark >= 65) && (mark <= 74))
grade=B;
else if ((mark >= 55) && (mark <= 64))
grade=C;
else if ((mark >= 40) && (mark <= 54))
grade=D;
else if ((mark >= 0) && (mark <= 39))
grade=E;
if ((mark > 100) || (mark < 0))
printf(Input error\n);
else
TK1913-C Programming
printf(Your grade is %c, grade);
33
Nested ifs
TK1913-C Programming
34
Nested ifs
Example:
35
TK1913-C Programming
36
TK1913-C Programming
Syntax:
switch switch
(expression)
{
(expression)
{
case expression1:
statement1;
break;
case espression2:
statement2;
break;
default:
expressionX;
break;
}
TK1913-C Programming
39
Syntax:
Must be
switch (expression) {
INTEGER or
case expression1:
CHAR !
statement1;
break;
case espression2:
statement2;
break;
default:
expressionX;
break;
}
TK1913-C Programming
40
Assume month = 1,
so
printf(January\n);
break;
case 2:
case 3:
default:
here. Jump to
printf(March\n);
break;
printf(Others\n);
break;
January
_
End
_
}
printf(End);
TK1913-C Programming
41
March
printf(January\n);
break;
_
End
_
printf(February\n);
break;
case 3:
Assume month = 3,
printf(March\n);
so
break;
default:
}
printf(End);
TK1913-C Programming
here. Jump to
42
printf(January\n);
break;
printf(February\n);
Nowwhat
No more !!will
break;
case 3:
43
Assume
month = 2,
February
_ so
March
printf(January\n);
_ _
break;
End
printf(February\n);
printf(Others\n);
continues.
break; Thus, case
execution
this step is}printf(End);
executed . So
TK1913-C Programming
is terminated
here. Jump to
44
printf(January\n);
break;
printf(February\n);
Nowwhat will
happen if these
printf(March\n);
breaks are taken out
break;
default:
from the program?
printf(Others\n);
And
And
case 3:
if month = 34
1 ??
break;
}
printf(End);
TK1913-C Programming
45
Exercise
To practice what youve
learned, try exercises on
page
/* Do exercises from pg
155 -156 of
Pengaturcaraan C
TK1913-C Programming
46
47