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 multiple path if nested 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 T Code == ‘M’ Male F T Code == ‘F’ Female F Invalid code
Multiple selection – multiple path if (code == ‘M’) cout<<“Male”; else if (code == ‘F’) cout<<“Female”; cout<<“Invalid code”; Both type of indention is acceptable if (code == ‘M’) cout<<“Male”; else if (code == ‘F’) cout<<“Female”; else cout<<“Invalid code”;
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) cout<<“teenager”; else 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”; A group of one-way selection
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 Program C Computer Science A Accounting E Engineering
Exercise Problem statement: Write a C++ program to determine the grade of a student based on the marks. ((mark>=0)&&(mark<=49)) ((mark>=50)&&(mark<=64)) Atau (mark>=75) (mark>=65) Marks Program 0 -49 F 50 -64 C 65 – 74 B 75 - 100 A
Multiple selection – nested if Three shell necklaces nested together
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 Message Person younger than 18 years old “you are too young to vote” Person is at least 18 years old and is registered to vote “you can vote” Person is at least 18 years old but not registering to 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 display “you are too young to vote” End
Multiple selection – nested if Flowchart Enter age age >=18 You are too young to vote Enter registration status status == y You can vote You need to register false true false true
Multiple selection – nested if C++ syntax cin>>age; if (age < 18) cout<<“you are too young to vote”; else { cin>>status; if (status == ‘y’) cout<<“you can vote”; 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. C++ Program segment: if( code == ‘C’) cout<<“ ….”; else if ( code == ‘E’) . Code Program C Computer Science A Accounting E 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 Program C Computer Science A Accounting E Engineering
Predefined function - strcmp cout<< "please enter program:"; cin.getline(program, 20); if(program == “computer science”) cout<<“C”; else if (program == “Accounting”) cout<<“A”; else if (program == “Engineering”) cout<<“E”; else cout<<“invalid”; Is this the correct way???
Predefined function - strcmp strcmp will accept two sequence of characters. It will return an integer. The integer will be: Negative if s1 is less than s2 Zero is s1 and s2 are equal Positive if s1 is greater than s2 int y = strcmp(s1, s2);
Predefined function - strcmp How to use it??? The output: cout<< "please enter program:"; cin.getline(program, 20); int y = strcmp(program, “computer science”); if(y == 0) cout<<“yes”; else cout<<“no”;
Predefined function - strcmp How to use it?? cout<< "please enter program:"; cin.getline(program, 20); if(strcmp(program, “computer science”) == 0) cout<<“yes”; else cout<<“no”;
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”;
Thank You !