Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science Department FTSM Control Structure: Selection (Part 2) Knowledge: Understand various concepts of selection control structure Skill: Be.

Similar presentations


Presentation on theme: "Computer Science Department FTSM Control Structure: Selection (Part 2) Knowledge: Understand various concepts of selection control structure Skill: Be."— Presentation transcript:

1 Computer Science Department FTSM Control Structure: Selection (Part 2) Knowledge: Understand various concepts of selection control structure Skill: Be able to develop a program containing selection control structure

2 TK1913-C Programming2 TK1913-C Programming 2 if Statements if if - else if – else - if

3 TK1913-C Programming3 TK1913-C Programming 3 if Statement The structure is similar to single selection (flowchart) Syntax: if (expression) statement; or if (expression) { statement1;statement2;} Syntax: if (expression) statement; or if (expression) { statement1;statement2;} Don’t forget the curly brackets !! Don’t forget the brackets !!

4 TK1913-C Programming4 TK1913-C Programming 4 if Statement if The similarity between single selection structure and if statement: Single Selection: if start step 1 step 2 … step k end_if Single Selection: if start step 1 step 2 … step k end_if if Statement: if ( ) { statement 1 statement 2 … statement k } if Statement: if ( ) { statement 1 statement 2 … statement k }

5 TK1913-C Programming5 TK1913-C Programming 5 if Statement Example: 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); Example: 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: _ num2 ? num1 ? min ? Key-in 2 numbers: 20 15 _ 20 15 2015 Key-in 2 numbers: 20 15 Smallest: 15 _ 20 > 15?

6 TK1913-C Programming6 TK1913-C Programming 6 if Statement Example: int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark > 80) { printf(“Category: Excellent\n”); printf(“Congratulations!”); } Example: int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark > 80) { printf(“Category: Excellent\n”); printf(“Congratulations!”); } Mark: _ mark ? 92 > 80? 92 Mark: 92 _ Mark: 92 Category: Excellent Mark: 92 Category: Excellent Congratulations!

7 TK1913-C Programming7 TK1913-C Programming 7 if Statement Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); printf(“Your mark is %d”, mark); } Example: 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?

8 TK1913-C Programming8 TK1913-C Programming 8 if Statement Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Pass\n”); printf(“Your mark is %d”, mark); } Example: 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?

9 TK1913-C Programming9 TK1913-C Programming 9 if - else Statement The structure is similar to double selection (flowchart) Syntax: if (expression) statement;elsestatement; or if (expression) { statement1;statement2; } else statement3; Syntax: if (expression) statement;elsestatement; or if (expression) { statement1;statement2; } else statement3;

10 TK1913-C Programming10 TK1913-C Programming 10 if - else Statement or if (expression) { statement1;statement2; } else { statement3;statement4;} or if (expression) { statement1;statement2; } else { statement3;statement4;}

11 TK1913-C Programming11 TK1913-C Programming 11 if – else Statement if - else The similarity between double selection structure and if - else statement: Double Selection: if start step 1 … step k end_if else start step 1 … step n end_else Double Selection: if start step 1 … step k end_if else start step 1 … step n end_else if Statement: if { statement 1 … statement k } else { statement 1 … statement n } if Statement: if { statement 1 … statement k } else { statement 1 … statement n }

12 TK1913-C Programming12 TK1913-C Programming 12 if - else Statement Example: if (num1 < num2) min = num1; else min = num2; printf(“Smallest: %d\n”, min); Example: if (num1 < num2) min = num1; else min = num2; printf(“Smallest: %d\n”, min); num2 15 num1 10 min ? 10 < 15? _ 10 Smallest: 10 _

13 TK1913-C Programming13 TK1913-C Programming 13 if - else Statement Example: if (num1 < num2) min = num1; else min = num2; printf(“Smallest: %d\n”, min); Example: if (num1 < num2) min = num1; else min = num2; printf(“Smallest: %d\n”, min); num2 15 num1 20 min ? 20 < 15? _ 15 Smallest: 15 _

14 TK1913-C Programming14 TK1913-C Programming 14 if - else Statement Example: if (num1 < num2) { min = num1; max = num2; } else { min = num2; max = num1; } printf(“Min = %d, Max = %d\n”, min, max); Example: if (num1 < num2) { min = num1; max = num2; } else { min = num2; max = num1; } printf(“Min = %d, Max = %d\n”, min, max); num2 125 num1 700 min ?? 700 < 125? _ max ?? Min = 125, Max = 700 _ 125 700

15 TK1913-C Programming15 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); } 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?

16 TK1913-C Programming16 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); } 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?

17 TK1913-C Programming17 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); } 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?

18 TK1913-C Programming18 TK1913-C Programming 18 Take a break …. Why we need program styles?….To ensure your program is readable. Let’s look some examples.. Let’s have a minute break. While you’re having a break, let me introduce you some program styles…..

19 TK1913-C Programming19 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); } 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); }

20 TK1913-C Programming20 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); } 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??

21 TK1913-C Programming21 TK1913-C Programming 21 Syntax: if (expression) statement; Syntax: if (expression) statement; if statement Let’s recap … Syntax: if (expression) statement;elsestatement; Syntax: if (expression) statement;elsestatement; if-else statement Ok now, let’s look at if – else –if statement

22 TK1913-C Programming22 TK1913-C Programming 22 if – else - if Statement Syntax: if (expression) statement; else if (expression) statement; Syntax: if (expression) statement; else if (expression) statement; if-else-if statement Syntax: if (expression) statement; else if (expression) statement; statement;elsestatement; Syntax: if (expression) statement; else if (expression) statement; statement;elsestatement; if-else-if statement

23 TK1913-C Programming23 TK1913-C Programming 23 if – else - if Statement Syntax: if (expression) statement; else if (expression) statement; statement; else (expression) statement; Syntax: if (expression) statement; else if (expression) statement; statement; else (expression) statement; if-else-if statement Be careful…common mistake made by students !!

24 TK1913-C Programming24 TK1913-C Programming 24 Let’s recap … Example: if start step m … end_if if start step n … end_if else start step x … end_else Example: if start step m … end_if if start step n … end_if else start step x … end_else Multiple Selection Assume condition 1 is true, so … step m, step …. will be executed

25 TK1913-C Programming25 TK1913-C Programming 25 Let’s recap … Example: if start step m … end_if if start step n … end_if else start step x … end_else Example: if start step m … end_if if start step n … end_if else start step x … end_else Multiple Selection Assume condition 1 is false, so … step m, step …. will be skipped, andstep m, step …. will be skipped, and condition 2 will be testedcondition 2 will be tested

26 TK1913-C Programming26 TK1913-C Programming 26 Let’s recap … Example: if start step m … end_if if start step n … end_if else start step x … end_else Example: if start step m … end_if if start step n … end_if else start step x … end_else Multiple Selection Assume condition 2 is true, so … step n, step …. will be executed

27 TK1913-C Programming27 TK1913-C Programming 27 Let’s recap … Example: if start step m … end_if if start step n … end_if else start step x … end_else Example: if start step m … end_if if start step n … end_if else start step x … end_else Multiple Selection Assume condition 2 is also false, so … step n, step …. will be skipped, andstep n, step …. will be skipped, and step x will be executedstep x will be executed

28 TK1913-C Programming28 TK1913-C Programming 28 Multiple Selection in C Example: #include 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”); } Example: #include 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?

29 TK1913-C Programming29 TK1913-C Programming 29 Multiple Selection in C Example: #include 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”); } Example: #include 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) 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) false (the letter is a digit) false

30 TK1913-C Programming30 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 < 100grade = A 64 < mark < 75grade = B 54 < mark < 65grade = C 39 < mark < 55grade = D 0 < mark < 40grade = E others error message

31 TK1913-C Programming31 TK1913-C Programming 31 Answer1Answer1Answer1Answer1 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”);

32 TK1913-C Programming32 TK1913-C Programming 32 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 ); Answer2Answer2Answer2Answer2

33 TK1913-C Programming33 TK1913-C Programming 33 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); Answer3Answer3Answer3Answer3

34 TK1913-C Programming34 TK1913-C Programming 34 Nested ifs if ifif - else if statement that contains other if / if - else statements

35 TK1913-C Programming35 TK1913-C Programming 35 Nested ifs Example: 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 */ else price = 1.50; /* for children & teenagers*/ } Example: 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 */ else price = 1.50; /* for children & teenagers*/ } This price is valid for people: age > 55 This price is valid for people: 18 < age < 55 This price is valid for people: age < 1 This price is valid for people: 1 < age < 18

36 TK1913-C Programming36 TK1913-C Programming 36 Nested ifs - Problem Example: if (age > 18) if (age > 55) price = 2.50; /* Price for senior citizens */ else price = 5.00; /* Price for adults */ 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?

37 TK1913-C Programming37 TK1913-C Programming 37 Nested ifs - Problem 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; } 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; Which one?

38 TK1913-C Programming38 TK1913-C Programming 38 Nested ifs - Problem else if 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; 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; }

39 TK1913-C Programming39 TK1913-C Programming 39 switch and break The structure is similar to multiple selection (flowchart) Syntax: switch (expression) { case expression1: statement1;break; case espression2: statement2;break;…default:expressionX;break;} Syntax: switch (expression) { case expression1: statement1;break; case espression2: statement2;break;…default:expressionX;break;} Syntax: switch (expression) { case expression1: statement1;break; case espression2: statement2;break;… default: expressionX;break;} Syntax: switch (expression) { case expression1: statement1;break; case espression2: statement2;break;… default: expressionX;break;} Don’t forget the brackets !! Don’t forget the curly brackets !! Don’t forget the colons !!

40 TK1913-C Programming40 TK1913-C Programming 40 switch and break Important Rule !! Syntax: switch (expression) { case expression1: statement1;break; case espression2: statement2;break;… default: expressionX;break;} Syntax: switch (expression) { case expression1: statement1;break; case espression2: statement2;break;… default: expressionX;break;} Must be INTEGER or CHAR !

41 TK1913-C Programming41 TK1913-C Programming 41 switch and break Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); break; case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); break; case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); Assume month = 1, so … …this step will be executed. Later … …case is terminated here. Jump to … January _ January End _

42 TK1913-C Programming42 TK1913-C Programming 42 switch and break Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); break; case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); break; case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); …this step will be executed. Later … March _ March End _ Assume month = 3, so … …case is terminated here. Jump to …

43 TK1913-C Programming43 TK1913-C Programming 43 switch and break Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); break; case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); break; case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); Now…what will happen if this break is taken out from the program? Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); No more !!

44 TK1913-C Programming44 TK1913-C Programming 44 switch and break Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); …this step will be executed. Later … February _ February March _ Assume month = 2, so … …case is terminated here. Jump to … February March End _ …execution continues. Thus, this step is executed. So …

45 TK1913-C Programming45 TK1913-C Programming 45 switch and break Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); Example : switch (month) { case 1: printf(“January\n”); break; case 2: printf(“February\n”); case 3: printf(“March\n”); break; default: printf(“Others\n”); break; } printf(“End”); Now…what will happen if these breaks are taken out from the program? And … if month = 1 ? And … if month = 34 ?

46 TK1913-C Programming46 TK1913-C Programming 46 Exercise To practice what you’ve learned, try exercises on page … /* Do exercises from pg 155 -156 of Pengaturcaraan C

47 TK1913-C Programming47 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…


Download ppt "Computer Science Department FTSM Control Structure: Selection (Part 2) Knowledge: Understand various concepts of selection control structure Skill: Be."

Similar presentations


Ads by Google