Download presentation
Presentation is loading. Please wait.
1
Control Structure: Selection (Part 2)
Knowledge: Understand various concepts of selection control structure Skill: Be able to develop a program containing selection control structure FTSM Computer Science Department
2
if Statements if if – else - if if - else TK1913-C Programming
3
Don’t forget the brackets !! Don’t forget the curly brackets !!
if Statement The structure is similar to single selection (flowchart) Syntax: if (expression) statement; or if (expression) { statement1; statement2; } Don’t forget the brackets !! Don’t forget the curly brackets !! TK1913-C Programming
4
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 if Statement: if (<condition>) { statement 1 statement 2 … statement k } TK1913-C Programming
5
if Statement 20 15 ? 20 > 15? 15 20 Example: num1 num2 min
int num1, num2, min; printf(“Key-in 2 numbers: “); scanf(“%d%d”, &num1, &num2); min = num1; if (num1 > num2) min = num2; printf(“Smallest: %d\n”, min); Key-in 2 numbers: 20 15 _ 20 15 num2 ? num1 min 20 > 15? 15 20 Key-in 2 numbers: _ Key-in 2 numbers: 20 15 Smallest: 15 _ TK1913-C Programming
6
if Statement 92 > 80? 92 ? Example: mark int mark;
printf(“Mark: “); scanf(“%d”, &mark); if (mark > 80) { printf(“Category: Excellent\n”); printf(“Congratulations!”); } 92 > 80? 92 Mark: 92 _ mark ? Mark: 92 Category: Excellent Congratulations! Mark: 92 Category: Excellent Mark: _ TK1913-C Programming
7
if Statement Example: What will the output be if the mark is 65?
void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); printf(“Your mark is %d”, mark); } What will the output be if the mark is 65? TK1913-C Programming
8
if Statement Example: What will the output be if the mark is 35?
void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); printf(“Your mark is %d”, mark); } What will the output be if the mark is 35? TK1913-C Programming
9
if - else Statement The structure is similar to double selection (flowchart) Syntax: if (expression) statement; else or if (expression) { statement1; statement2; } else statement3; TK1913-C Programming
10
if - else Statement or if (expression) { statement1; statement2;
} TK1913-C Programming
11
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 n end_else if Statement: if <condition> { statement 1 … statement k } else { statement n TK1913-C Programming
12
if - else Statement Example: 15 10 ? 10 < 15? 10
if (num1 < num2) min = num1; else min = num2; printf(“Smallest: %d\n”, min); num2 15 num1 10 min ? 10 < 15? 10 Smallest: 10 _ _ TK1913-C Programming
13
if - else Statement Example: 15 20 ? 20 < 15? 15
if (num1 < num2) min = num1; else min = num2; printf(“Smallest: %d\n”, min); num2 15 num1 20 min ? 20 < 15? 15 Smallest: 15 _ _ TK1913-C Programming
14
if - else Statement Example: 700 700 < 125? 125 ?? 125 ?? 700 num1
if (num1 < num2) { min = num1; max = num2; } else { min = num2; max = num1; printf(“Min = %d, Max = %d\n”, min, max); num1 700 700 < 125? num2 125 min ?? 125 max ?? 700 _ Min = 125, Max = 700 _ TK1913-C Programming
15
if – else Statement Example:
void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); else printf(“Fail\n”); printf(“Your mark is %d”, mark); } What will the output be if the mark is 21? What will the output be if the mark is 74? TK1913-C Programming
16
if – else Statement Example:
void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); else printf(“Fail\n”); printf(“Your mark is %d”, mark); } What will the output be if the mark is 74? What will the output be if the mark is 14? TK1913-C Programming
17
if – else Statement Example:
void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); else { printf(“Fail\n”); printf(“Your mark is %d”, mark); } What will the output be if the mark is 14? What will the output be if the mark is 70? TK1913-C Programming
18
Take a break …. Let’s have a minute break.
While you’re having a break, let me introduce you some program styles….. Why we need program styles?….To ensure your program is readable. Let’s look some examples.. TK1913-C Programming
19
Take a break … (Learn styles)
Example: void main() { int mark; 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
Take a break … (Learn styles)
Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); else printf(“Fail\n”); printf(“Your mark is %d”, mark); } Difficult to read!!! Don’t you think so?? TK1913-C Programming
21
Let’s recap … Syntax: if (expression) statement; else
if-else statement Syntax: if (expression) statement; if statement Ok now, let’s look at if – else –if statement TK1913-C Programming
22
if – else - if Statement Syntax: if (expression) statement;
else if (expression) else if-else-if statement Syntax: if (expression) statement; else if (expression) if-else-if statement TK1913-C Programming
23
Be careful…common mistake made by students !!
if – else - if Statement Syntax: if (expression) statement; else if (expression) else (expression) if-else-if statement Be careful…common mistake made by students !! TK1913-C Programming
24
Assume condition 1 is true, so … step m, step …. will be executed
Let’s recap … Example: if <condition_1 is true> start step m … end_if if <condition_2 is true> start step n else start step x end_else Multiple Selection Assume condition 1 is true, so … step m, step …. will be executed TK1913-C Programming
25
Assume condition 1 is false, so …
Let’s recap … Example: if <condition_1 is true> start step m … end_if if <condition_2 is true> start step n else start step x end_else Multiple Selection Assume condition 1 is false, so … step m, step …. will be skipped, and condition 2 will be tested TK1913-C Programming
26
Assume condition 2 is true, so … step n, step …. will be executed
Let’s recap … Example: if <condition_1 is true> start step m … end_if if <condition_2 is true> start step n else start step x end_else Multiple Selection Assume condition 2 is true, so … step n, step …. will be executed TK1913-C Programming
27
Assume condition 2 is also false, so …
Let’s recap … Example: if <condition_1 is true> start step m … end_if if <condition_2 is true> start step n else start step x end_else Multiple Selection Assume condition 2 is also false, so … step n, step …. will be skipped, and step x will be executed TK1913-C Programming
28
Multiple Selection in C
Example: #include <stdio.h> int main( ) { char letter; scanf(“%c”, &letter); if (letter >= ‘a’ && letter <= ‘z’ ) printf(“Lower case\n”); else if (letter >= ‘A’ && letter <= ‘Z’) printf(“Upper case\n”); else if (letter >= ‘0’ && letter <= ‘9’) printf(“Digit\n”); else printf(“Special character\n”); } Is the letter a lower case? Is the letter an upper case? Is the letter a digit? TK1913-C Programming
29
Multiple Selection in C
Example: #include <stdio.h> int main( ) { char letter; scanf(“%c”, &letter); if (letter >= ‘a’ && letter <= ‘z’ ) printf(“Lower case\n”); else if (letter >= ‘A’ && letter <= ‘Z’) printf(“Upper case\n”); else if (letter >= ‘0’ && letter <= ‘9’) printf(“Digit\n”); else printf(“Special character\n”); } (the letter is a lower case) true (the letter is a lower case) false (the letter is an upper case) false (the letter is a digit) true (the letter is a lower case) false (the letter is an upper case) true (the letter is a lower case) false (the letter is an upper case) false (the letter is a digit) false TK1913-C Programming
30
Exercise Develop a program for the following problem.
Given a mark, determine its grade based on the table below: 74 < mark < 100 grade = A 64 < mark < 75 grade = B 54 < mark < 65 grade = C 39 < mark < 55 grade = D 0 < mark < 40 grade = E others error message TK1913-C Programming
31
A n s w e r 1 printf(“Key-in the mark: “); scanf(“%d”,&mark);
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”); A n s w e r 1 TK1913-C Programming
32
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 ); A n s w e r 2 TK1913-C Programming
33
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 printf(“Your grade is %c”, grade); A n s w e r 3 TK1913-C Programming
34
if statement that contains other if / if - else statements
Nested ifs if statement that contains other if / if - else statements TK1913-C Programming
35
Nested ifs Example: This price is valid for people: age > 55
if (age > 18) { if (age > 55) price = 2.50; /* Price for senior citizens */ else price = 5.00; /* Price for adults */ } else { if (age < 1) price = 0.0; /* Price for infants */ price = 1.50; /* for children & teenagers*/ This price is valid for people: age < 1 This price is valid for people: 1 < age < 18 TK1913-C Programming
36
Which if does this else belong to?
Nested ifs - Problem Example: if (age > 18) if (age > 55) price = 2.50; /* Price for senior citizens */ else price = 5.00; /* Price for adults */ Which if does this else belong to? TK1913-C Programming
37
Nested ifs - Problem Which one? if (age > 18) { if (age > 55)
price = 2.50; } else price = 5.00; if (age > 18) { if (age > 55) price = 2.50; else price = 5.00; } TK1913-C Programming
38
Nested ifs - Problem By default, else will be attached to the nearest if if (age > 18) { if (age > 55) price = 2.50; else price = 5.00; } if (age > 18) if (age > 55) price = 2.50; else price = 5.00; TK1913-C Programming
39
switch and break switch (expression) { Don’t forget the brackets !!
The structure is similar to multiple selection (flowchart) Syntax: switch (expression) { case expression1: statement1; break; case espression2: statement2; … default: expressionX; } Syntax: switch (expression) { case expression1: statement1; break; case espression2: statement2; … default: expressionX; } Don’t forget the brackets !! Don’t forget the curly brackets !! Don’t forget the colons !! TK1913-C Programming
40
switch and break Must be INTEGER or CHAR ! Important Rule !! Syntax:
switch (expression) { case expression1: statement1; break; case espression2: statement2; … default: expressionX; } Must be INTEGER or CHAR ! TK1913-C Programming
41
switch and break Assume month = 1, so …
Example: switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); case 3: printf(“March\n”); default: printf(“Others\n”); } printf(“End”); Assume month = 1, so … …this step will be executed. Later … …case is terminated here. Jump to … January End _ January _ TK1913-C Programming
42
switch and break Assume month = 3, so …
Example: switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); case 3: printf(“March\n”); default: printf(“Others\n”); } printf(“End”); Assume month = 3, so … March _ March End _ …this step will be executed. Later … …case is terminated here. Jump to … TK1913-C Programming
43
Now…what will happen if this break is taken out from the program?
switch and break Example: switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); case 3: printf(“March\n”); default: printf(“Others\n”); } printf(“End”); Example: switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); case 3: printf(“March\n”); default: printf(“Others\n”); } printf(“End”); Now…what will happen if this break is taken out from the program? No more !! TK1913-C Programming
44
switch and break Assume month = 2, so …
Example: switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); case 3: printf(“March\n”); default: printf(“Others\n”); } printf(“End”); February March _ February March End _ February _ …this step will be executed. Later … …execution continues. Thus, this step is executed . So … …case is terminated here. Jump to … TK1913-C Programming
45
Now…what will happen if these breaks are taken out from the program?
switch and break Example: switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); case 3: printf(“March\n”); default: printf(“Others\n”); } printf(“End”); Now…what will happen if these breaks are taken out from the program? And … if month = 34 ? And … if month = 1 ? TK1913-C Programming
46
Exercise To practice what you’ve learned, try exercises on page …
/* Do exercises from pg of Pengaturcaraan C TK1913-C Programming
47
End of Lecture 7 (Part 2) At last, chap 7 is finished. What’s next???
Control Structure: Repetition on the way… TK1913-C Programming
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.