Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
MULTIPLE SELECTION To solve a problem that has several selection, use either of the following method: Multiple selection nested if multiple path if
Multiple selection multiple path
Multiple selection multiple path Problem statement: Get a character from the user, if the users enter the letter M, display Male, else if the user enter F display Female, else display invalid code ;
Multiple selection multiple path Pseudocode if the code equal to M then print Male else if the code equal to F then print Female else print Invalid code
Multiple selection multiple path Flowchart Code == M F Code == F F T T Male Female Invalid code
Multiple selection multiple path if (code == M ) cout<< Male ; else if (code == F ) cout<< Female ; else cout<< Invalid code ; if (code == M ) cout<< Male ; else if (code == F ) cout<< Female ; else cout<< Invalid code ; Both type of indention is acceptable
Exercise Write a pseudocode and flowchart for the following C++ program segment. cin>>age; if (age <=10) cout<< child ; else if (age >10)&&(age<21) else cout<< teenager ; cout<< adult ;
Exercise Can this be done??. cin>>age; if (age <=10) cout<< child ; if (age >10)&&(age<21) cout<< teenager ; if (age>=21) cout<< adult ;
Exercise Problem statement: Write a C++ program to display the program of the student based on the code the he or she inputs in the computer. Display Invalid code if the student enter wrong code. Code C A E Program Computer Science Accounting Engineering
Exercise Problem statement: Write a C++ program to determine the grade of a student based on the marks. Marks Program 0-49 F 50-64 C 65 74 B 75-100 A
Multiple selection nested if
Multiple selection nested if Problem statement: You want to create voter eligibility program that display that displays one of three messages. The messages and the criteria for displaying each message are shown here: Criteria Person younger than 18 years old Person is at least 18 years old and is registered to vote Person is at least 18 years old but not registering to vote Message you are too young to vote you can vote you need to register before you can vote
Multiple selection nested if Pseudocode: Begin Enter age if ( the age greater than or equal to 18)then enter registration status if (registration status equal to y)then display you can vote else display you need to register before voting end if else display you are too young to vote end if End
Multiple selection nested if Flowchart Enter age false You are too young to vote age >=18 true Enter registration status You need to register false status == y true You can vote
Multiple selection nested if C++ syntax cin>>age; if (age < 18) else { cout<< you are too young to vote ; cin>>status; if (status == y ) cout<< you can vote ; else cout<< you need to register ; //end if }//end if
Predefined function - strcmp The strcmp are in the library with the header file string. #include<string>
Predefined function - strcmp Problem statement: Write a C++ program to display the program of the student based on the code the he or she inputs in the computer. Display Invalid code if the student enter wrong code. Code C Program Computer Science C++ Program segment: if( code == C ) cout<<. ; else if ( code == E ) cout<<. ;... A E Accounting Engineering
Predefined function - strcmp Write a C++ program to display the code of the student based on the program the he or she inputs in the computer. Display Invalid program if the student enter wrong program. Code C A E Program Computer Science Accounting Engineering
Characters ASCII codes
Predefined function - strcmp cout<< "please enter program:"; cin.getline(program, 20); if(strcmp(program, computer science ) == 0) cout<< C ; else if (strcmp(program, Accounting ) == 0) cout<< A ; else if (strcmp(program, Engineering ) == 0) cout<< E ; else cout<< invalid ;
switch Structures switch structure: alternate to if-else switch (integral) expression is evaluated first value of the expression determines which corresponding action is taken expression is sometimes called the selector C++ Programming: From Problem Analysis to Program Design, Fourth Edition 22
switch Structures (continued) one or more statements may follow a case label braces are not needed to turn multiple statements into a single compound statement the break statement may or may not appear after each statement switch, case, break, and default are reserved words switch statements case labels identify specific code segments can use a variety of data types as case labels break statement used to exit switch statements default label contains statements that execute when the condition expression doesn t match any of the case labels C++ Programming: From Problem Analysis to Program Design, Fourth Edition 24
Exercise Write a program in switch statements that accepts a code of type integer and determines the name of the program for that code based on the following table. Code Program 1 Diploma in Engineering 2 Diploma in Computer Science Sample Output 1 Enter Code [1, 2, 3 or 4] : 4 Diploma in Banking End of Program. Thank you. Sample Output 2 Enter Code [1, 2, 3 or 4] : 6 Invalid Code End of Program. Thank you. 3 Diploma in Business Study 4 Diploma in Banking
switch Exercise Convert following C++ nested if statements into switch statements
switch Exercise ANSWERS
switch Exercise Write a program fragment that accepts a grade of type characternand determines the range of score for that grade as shown below. Grade Score Range A 90 100 B 80 89 C 70 79 D 60 69 E 0-59 If the grade entered is other than the above, an error message will be displayed.
switch Exercise Using a switch statement, write a program that reads two integers and a character. The operation to be performed is based on the character input, as indicated in the table below:- Character Input Operation + Display the sum of two numbers - Display the difference / Display the quotient * Display the product % Display the remainder