Module 9 Selection Structures ITE 102 Computer Programming

  • Slides: 46
Download presentation
Module 9: Selection Structures ITE 102 – Computer Programming (C++)

Module 9: Selection Structures ITE 102 – Computer Programming (C++)

if Statements if if – else - if if - else Introduction to Computer

if Statements if if – else - if if - else Introduction to Computer Programming

if Statement The structure is similar to single selection (flowchart) Syntax: Don’t forget the

if Statement The structure is similar to single selection (flowchart) Syntax: Don’t forget the if (expression) brackets statement; or if (expression) { Don’t forget the statement 1; curly brackets statement 2; }

if Statement The similarity between single selection structure and if statement: Single Selection: if

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 Introduction to Computer Programming if Statement: if (<condition>) { statement 1 statement 2 … statement k }

if Statement Example: int num 1, num 2, min; printf(“Key-in 2 numbers: “); 20

if Statement Example: int num 1, num 2, min; printf(“Key-in 2 numbers: “); 20 > 15? scanf(“%d%d”, &num 1, &num 2); min = num 1; if (num 1 > num 2) min = num 2; printf(“Smallest: %dn”, min); Key-in 2 numbers: 20 _ 15 _ Smallest: 15 _ Introduction to Computer Programming num 1 20 ? num 2 15 ? min 15 20 ?

if Statement Example: int grd; 92 > 80? printf(“Grade: “); scanf(“%d”, &grd); if (grd

if Statement Example: int grd; 92 > 80? printf(“Grade: “); scanf(“%d”, &grd); if (grd > 80) { printf(“Category: Excellentn”); printf(“Congratulations!”); } Mark: Grade: 92 _ Category: Excellent Congratulations! Introduction to Computer Programming grd ? 92

if Statement Example: void main() { What will the output be if the mark

if Statement Example: void main() { What will the output be if the mark is 65? int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Passn”); printf(“Your mark is %d”, mark); } Introduction to Computer Programming

if Statement Example: void main() { int mark; What will the output be if

if Statement Example: void main() { int mark; What will the output be if the mark is 35? printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Passn”); printf(“Your mark is %d”, mark); } Introduction to Computer Programming

if - else Statement The structure is similar to double selection (flowchart) Syntax: or

if - else Statement The structure is similar to double selection (flowchart) Syntax: or if (expression) statement; else statement; if (expression) { statement 1; statement 2; } else statement 3; Introduction to Computer Programming

if - else Statement or if (expression) { statement 1; statement 2; } else

if - else Statement or if (expression) { statement 1; statement 2; } else { statement 3; statement 4; } Introduction to Computer Programming

if – else Statement The similarity between double selection structure and if - else

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 Introduction to Computer Programming if Statement: if <condition> { statement 1 … statement k } else { statement 1 … statement n }

if - else Statement Example: if (num 1 < num 2) 10 < 15?

if - else Statement Example: if (num 1 < num 2) 10 < 15? min = num 1; else min = num 2; printf(“Smallest: %dn”, min); _ Smallest: 10 _ Introduction to Computer Programming num 1 10 num 2 15 min 10 ?

if - else Statement Example: if (num 1 < num 2) 20 < 15?

if - else Statement Example: if (num 1 < num 2) 20 < 15? min = num 1; else min = num 2; printf(“Smallest: %dn”, min); _ Smallest: 15 _ Introduction to Computer Programming num 1 20 num 2 15 min 15 ?

if - else Statement Example: num 1 700 < 125? if (num 1 <

if - else Statement Example: num 1 700 < 125? if (num 1 < num 2) { min = num 1; num 2 125 max = num 2; } min 125 ? ? else { min = num 2; max = num 1; max 700 ? ? } printf(“Min = %d, Max = %dn”, min, max); _ = 125, Max = 700 Min _ Introduction to Computer Programming

if – else Statement Example: void main() { int mark; What will the output

if – else Statement Example: void main() { int mark; What will the output be if the mark is 21? printf(“Mark: “); scanf(“%d”, &mark); What will the output be if the mark is 74? if (mark >= 50) printf(“Passn”); else printf(“Failn”); printf(“Your mark is %d”, mark); } Introduction to Computer Programming

if – else Statement Example: void main() { int mark; What will the output

if – else Statement Example: void main() { int mark; What will the output be if the mark is 74? printf(“Mark: “); scanf(“%d”, &mark); What will the output be if the mark is 14? if (mark >= 50) printf(“Passn”); else printf(“Failn”); printf(“Your mark is %d”, mark); } Introduction to Computer Programming

if – else Statement Example: What will the output be void main() { if

if – else Statement Example: What will the output be void main() { if the mark is 14? int mark; printf(“Mark: “); scanf(“%d”, &mark); What will the output be if (mark >= 50) if the mark is 70? printf(“Passn”); else { printf(“Failn”); printf(“Your mark is %d”, mark); } } Introduction to Computer Programming

Learn styles… Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark

Learn styles… Example: void main() { int mark; printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Passn”); else printf(“Failn”); printf(“Your mark is %d”, mark); } Introduction to Computer Programming

Take a break … (Learn styles) Example: Difficult to read!!! void main() { int

Take a break … (Learn styles) Example: Difficult to read!!! void main() { int mark; Don’tso? ? you think printf(“Mark: “); scanf(“%d”, &mark); if (mark >= 50) printf(“Passn”); else printf(“Failn”); printf(“Your mark is %d”, mark); } Introduction to Computer Programming

Let’s recap … Syntax: if ((expression) statement; else statement; if-else if statement Ok now,

Let’s recap … Syntax: if ((expression) statement; else statement; if-else if statement Ok now, let’s look at if – else –if statement Introduction to Computer Programming

if – else - if Statement Syntax: if (expression) statement; else statement; Introduction to

if – else - if Statement Syntax: if (expression) statement; else statement; Introduction to Computer Programming if-else-if statement

if – else - if Statement Syntax: if (expression) statement; else (expression) statement; Introduction

if – else - if Statement Syntax: if (expression) statement; else (expression) statement; Introduction to Computer Programming if-else-if statement Be careful…common mistake made by students

Let’s recap … Example: Multiple Selection if <condition_1 is true> start step m …

Let’s recap … Example: Multiple Selection if <condition_1 is true> start step m … step m, step …. will 1 be Assume condition is end_if executed true, so … if <condition_2 is true> start step n … end_if else start step x … end_else Introduction to Computer Programming

Let’s recap … Example: Multiple Selection if <condition_1 is true> start step m …

Let’s recap … Example: Multiple Selection if <condition_1 is true> start step m … • Assume step m, step …. will be condition 1 is end_if skipped, andso … false, if <condition_2 is true> start step n • condition 2 will be tested … end_if else start step x … end_else Introduction to Computer Programming

Let’s recap … Example: Multiple Selection if <condition_1 is true> start step m …

Let’s recap … Example: Multiple Selection if <condition_1 is true> start step m … end_if if <condition_2 is true> start step n … Assume step n, step condition …. will 2 be is end_if true, executed so … else start step x … end_else Introduction to Computer Programming

Let’s recap … Example: Multiple Selection if <condition_1 is true> start step m …

Let’s recap … Example: Multiple Selection if <condition_1 is true> start step m … end_if if <condition_2 is true> start step n … • Assume step n, step …. will be condition 2 is end_if skipped, and so … also false, else start step x • step x will be executed … end_else Introduction to Computer Programming

Multiple Selection in C Example: #include <stdio. h> int main( ) { char letter;

Multiple Selection in C Example: #include <stdio. h> int main( ) { char letter; Is the letter a lower case? scanf(“%c”, &letter); Is the letter an upper if (letter >= ‘a’ && letter <= ‘z’ ) case? } printf(“Lower casen”); else if (letter >= ‘A’ && letter <= Is ‘Z’)the letter a digit? printf(“Upper casen”); else if (letter >= ‘ 0’ && letter <= ‘ 9’) printf(“Digitn”); else printf(“Special charactern”); Introduction to Computer Programming

Multiple Selection in C Example: #include <stdio. h> int main( ) { char letter;

Multiple Selection in C Example: #include <stdio. h> int main( ) { char letter; (the letter is a lower case) true scanf(“%c”, &letter); (theletteris isaalowercase) if (letter >= ‘a’ && letter <= ‘z’ ) false printf(“Lower casen”); (theletteris istrue an anuppercase) else if (letter >= ‘A’ &&(the letter <= ‘Z’)false is a lower case) (the letter false is a digit) true printf(“Upper casen”); else if (letter >= ‘ 0’ && letter <= ‘ 9’) (the letter isfalse an upper case) printf(“Digitn”); else } Introduction to Computer Programming (the letter is a digit) false printf(“Special charactern”);

Exercise Develop a program for the following problem. Given a mark, determine its grade

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 Introduction to Computer Programming grade = A grade = B grade = C grade = D grade = E error message

A n s w e r 1 int mark; printf(“Key-in the mark: “); scanf(“%d”,

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 errorn”); Introduction to Computer Programming

A n s w e r 2 int mark; char grade ; printf(“Key-in the

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 errorn”); printf(“Your grade is %c”, grade ); Introduction to Computer Programming

int mark; char grade; printf(“Key-in the mark: “); scanf(“%d”, &mark); if ((mark >= 75)

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 errorn”); else Introduction to Computer printf(“Your grade is %c”, grade); Programming A n s w e r 3

Nested ifs if statement that contains other if / if - else statements Introduction

Nested ifs if statement that contains other if / if - else statements Introduction to Computer Programming

Nested ifs Example: This price is valid for people: > 55 people: 18 age

Nested ifs Example: This price is valid for people: > 55 people: 18 age < 55 if (age > 18) { if (age > 55) price = 2. 50; /* Price for senior citizens */ This price is valid for else price = 5. 00; /* Price people: for adultsage */ < 1 } This price is valid for else { people: 1 < age < 18 if (age < 1) price = 0. 0; /* Price for infants */ else price = 1. 50; /* for children & teenagers*/ } Introduction to Computer Programming

Nested ifs - Problem Example: if (age > 18) if (age > 55) price

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? Introduction to Computer Programming

Nested ifs - Problem Which one? if (age > 18) { if (age >

Nested ifs - Problem Which one? if (age > 18) { if (age > 55) price = 2. 50; } else price = 5. 00; Introduction to Computer Programming if (age > 18) { if (age > 55) price = 2. 50; else price = 5. 00; }

Nested ifs - Problem By default, else will be attached to the nearest if

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; Introduction to Computer Programming if (age > 18) { if (age > 55) price = 2. 50; else price = 5. 00; }

Nested ifs - Problem By default, else will be attached to the nearest if

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; Introduction to Computer Programming if (age > 18) { if (age > 55) price = 2. 50; else price = 5. 00; }

switch and break The structure is similar to multiple selection (flowchart) Syntax: switch (expression)

switch and break The structure is similar to multiple selection (flowchart) Syntax: switch (expression) { case expression 1: statement 1; break; case espression 2: statement 2; break; … … default: : expression. X; break; }} Introduction to Computer Programming Don’t forget the brackets !! Don’t forget the curly colons brackets !! !!

switch and break Important Rule !! Syntax: Must be INTEGER switch (expression) { or

switch and break Important Rule !! Syntax: Must be INTEGER switch (expression) { or CHAR case expression 1: statement 1; break; case espression 2: statement 2; break; … default: expression. X; break; } Introduction to Computer Programming

switch and break Example: switch (month) { case 1: case 2: case 3: default:

switch and break Example: switch (month) { case 1: case 2: case 3: default: printf(“Januaryn”); break; …this step will be printf(“Februaryn”); …executed. case is terminated Later … break; here. Jump to … printf(“Marchn”); break; printf(“Othersn”); break; } printf(“End”); Introduction to Computer Programming Assume month = 1, so … January _ End _

switch and break Example: switch (month) { case 1: case 2: case 3: default:

switch and break Example: switch (month) { case 1: case 2: case 3: default: printf(“Januaryn”); break; _ End _ printf(“Februaryn”); break; Assume month = 3, printf(“Marchn”); so … break; …this step will be printf(“Othersn”); …executed. case is terminated Later … break; } printf(“End”); Introduction to Computer Programming March here. Jump to …

switch and break Example: switch (month) { case 1: case 2: printf(“Januaryn”); break; printf(“Februaryn”);

switch and break Example: switch (month) { case 1: case 2: printf(“Januaryn”); break; printf(“Februaryn”); Now…what No more !!will break; happen if this break is printf(“Marchn”); taken out from the break; program? default: case 3: printf(“Othersn”); break; } } printf(“End”); Introduction to Computer Programming

switch and break Example: switch (month) { case 1: case 2: case 3: default:

switch and break Example: switch (month) { case 1: case 2: case 3: default: Assume month = 2, February so … _ March printf(“Januaryn”); _ _ break; End printf(“Februaryn”); printf(“Marchn”); …this step will be executed. Later … break; printf(“Othersn”); …execution continues. break; Thus, …case is terminated here. Jump to … this step is} executed. So … printf(“End”); Introduction to Computer Programming

switch and break Example: switch (month) { case 1: case 2: printf(“Januaryn”); break; printf(“Februaryn”);

switch and break Example: switch (month) { case 1: case 2: printf(“Januaryn”); break; printf(“Februaryn”); Now…what will happen if these printf(“Marchn”); breaks are taken out break; from the program? default: And … ifif month = 34 1 ? ? case 3: printf(“Othersn”); break; } printf(“End”); Introduction to Computer Programming

END OF MODULE 9 Introduction to Computer Programming 46

END OF MODULE 9 Introduction to Computer Programming 46