Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection Control Structure

Similar presentations


Presentation on theme: "Selection Control Structure"— Presentation transcript:

1 Selection Control Structure
Week 4

2 Topic 3: Learning Outlines
Relational Operator Logical Operator Boolean Expression Types of Selection Control Structure

3 Used to make comparison between expression
Output: 0 or 1 / True or False Require two operands Relational Operator Math C++ Description = == If (a == b) < If(a < b) <= If(a <= b) > If(a > b) >= If(a >= b) != If(a != b)

4 if(strcmp (code, code2)==0){…}
Sample 1: int a = 8; int b = 19; if ( a != b ){…} if (a < = b){…} if (a >= b){…} Sample 2: char code [6] = “ “; cin.getline(code, 6); if(strcmp (code, “CSC128”)==0){…} Sample 3: char option = ‘ ’; cin >> option; if(option == ‘Y’){…} Sample 4: char code [6] = “ “; char code2[6]= “ ”; strcpy(code, “csc128”); strcpy(code2, “mat235”); if(strcmp (code, code2)==0){…}

5 To combine logical expression
Take only logical values as operands Logical Operator Output: logical value Operator Description Usage ! Not !a && And If(a > b && b < c) || Or If(a < b || b > c)

6 Truth table P Q P && Q P || Q !P !Q 1 1 1 1 1

7 Sample 1: cin >> a if (a > 0 && a < 51){…} if (a >= 1 && a <=50){…} if (a < 20 || a > 50){…} if (a <= 20 || a >= 50){…} Sample 2: char code [6] = “ “; cin.getline(code, 6); if(strcmp (code, “CSC128”)==0 || strcmp (code, “csc128”)==0){…} Sample 3: char option = ‘ ’; cin >> option; if(option == ‘Y’ || option == ‘y’){…} if(option != ‘N’ || option != ‘n’){…}

8 Precedence of operator
Precedence of operators (from highest to lowest) Parentheses ( … ) Unary operators ! Multiplicative operators * / % Additive operators Relational ordering < <= >= > Relational equality == != Logical and && Logical or || Assignment =

9 Boolean values bool bValue; bValue = true;
Boolean values only have two possible values: true (1) and false (0). To declare a boolean variable, we use the keyword bool (data type). when converting integers to booleans, the integer zero resolves to boolean false, whereas non-zero integers all resolve to true. bool bValue; bValue = true;

10 Sample 1: bool pilihan = true; if(pilihan
Sample 1: bool pilihan = true; if(pilihan != false){…} Sample 2: int x = 25, y = 50; bool answer; answer = x == y; cout<< answer;

11 Types of Decision Control Structure
One Way Two Ways Multi Ways Multiple Nested Switch...Case

12 One way selection The syntax of one-way selection is: if (expression)
statement; Statement is executed if the value of the expression is true (1) Statement is by passed if the value is false (0); program goes to the next statement

13 Boolean expression in if condition
if (quantity <50) if (age>=25) if (onhand == target) if (quantity != 7500)

14 One way selection Pseudocode: if (expression) then statements
Flowchart: if (expression) then statements end if Expression True False Statements

15 Example Problem statement:
If the speed of the car is greater than 80km/h, then output a message “you are too fast!” Start Begin input speed if speed > 80 output “you are too fast!” end if End Input speed T You are too fast F speed>80 End

16 Example #include <iostream.h> void main() { int speed;
cin >> speed; if (speed > 80) cout << “you are too fast!”; }

17 Compound statements Use the symbols { and } to group several statements as a single unit. Simple statements within the compound statements end with semicolons. Compound Statements are sometimes called a statement block. Use compound statements in an if statement if you want several actions executed when the Boolean expression is true.

18 Compound statements (cont.)
void main() { int mark; cin >> mark; if (mark >= 50) cout << “PASSED!”; cout<< “thank you”<<endl; } void main() { int mark; cin >> mark; if (mark >= 50) cout << “PASSED!”; cout<< “thank you”<<endl; } Single statement compound statements

19 Two way selection Two-way selection takes the form: if (expression)
statement1; else statement2; If expression is true, statement1 is executed otherwise statement2 is executed statement1 and statement2 are any C++ statements if and else is a reserved word

20 Two way selection Pseudocode: if (expression) then statement1 else
end if

21 Two way selection Flowchart: True False Statement1 Statement2
Expression True Statement1 False Statement2

22 Example Program segment Flowchart if (cgpa < 2.0) status = ‘F’;
else status = ‘P’; if (choice == 1) gender = ‘F’; gender = ‘M’; cgpa < 2.0 false true P F choice == 1 false true M F

23 Example Problem statement:
If student marks greater than or equal to 50, then output a message “pass”, else “fail”. Pseudocode: Begin input marks if marks >= 50 then output “pass” else output “fail” end if End

24 Example Flowchart Start Input marks marks>=40 T Pass F Fail End

25 Example #include <iostream.h> void main() { int marks;
cin >>marks; if (marks >= 50) cout << “pass”; else cout << “fail”; }

26 Compound statements

27 Compound statements (cont.)
if (marks >= 50) { cout << “pass”<<endl; cout << “Good job!!”; } else cout << “fail”; cout << “Try again!!”;

28 Compound statements (cont.)
Syntax and code: #include <iostream.h> void main() { int marks; cin >> marks; if (marks >= 50) cout << “pass”; else cout << “fail”; cout << “you have to have to repeat”; }

29 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”;

30 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”

31 Multiple selection – multiple path
Flowchart T Code == ‘M’ Male F T Code == ‘F’ Female F Invalid code

32 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”;

33 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”

34 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

35 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

36 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

37 Predefined function - strcmp
The strcmp are in the library with the header file string. #include<string>

38 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

39 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

40 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???

41 Predefined function - strcmp
int y = strcmp(s1, s2); 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

42 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”;

43 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”;

44 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”;

45 Switch…Case Switch case statement do not require the evaluation of a logical expression, compare with if else statement. Give the computer power to select from among many alternatives/options. The reverse words are: switch, case, break, default. Switch structure contain constant expression. Constant expression will be evaluated at first in the structure. It is used to select the action/option specified in the statement that follow the reserved word case. Normally, constant expression is the identifier. In such situation, it is also called as selector. Break is used to immediately exit from the switch structure.

46 General Form switch(condition){ case constant_expression: { statements; break; } default: single line statement; // no compound statement //if more than single line statement MUST have compound //statement

47 Example 1 – if…else statement using int
int num; cout << “Please select your menu number”; cin >> num; //condition statement (variable) if (num == 1){ //number 1 & 2 known as constant expression cout << “You have select menu number 1”; } else if(num == 2){//number 1 & 2 known as constant expression cout << “You have select menu number 2”; else cout << “Invalid selection”;

48 Example 1 – Constant Expression using int
int num; cout << “Please select your menu number”; cin >> num; //condition statement for switch (variable) switch(num){ case 1:{ //number 1 & 2 known as constant expression cout << “You have select menu number 1”; break; } case 2:{//number 1 & 2 known as constant expression cout << “You have select menu number 2”; default: cout << “Invalid selection”;

49 Example 2 – if…else statement using char
char character; cout << “Please select your menu”; cin >> character; //condition statement (variable) if (character == ‘A’){ //A & B known as constant expression cout << “You have select menu A”; } else if(character == ‘B’){//A & B known as constant expression cout << “You have select menu B”; else cout << “Invalid selection”;

50 Example 2 – Constant Expression using char
char character; cout << “Please select your menu”; cin >> character; //condition statement for switch (variable) switch(character){ case ‘A’:{ //A & B known as constant expression cout << “You have select menu A”; break; } case ‘B’:{//A & B known as constant expression cout << “You have select menu B”; default: cout << “Invalid selection”;

51 Example 3 – if…else statement using char
char character; cout << “Please select your menu”; cin >> character; //condition statement for switch (variable) if(character == ‘A’ || character == ‘a’){ //A & B known as constant expression cout << “You have select menu A”; } else if(character == ‘B’ || character == ‘b’) {//A & B known as constant expression cout << “You have select menu B”; else cout << “Invalid selection”;

52 Example 3 – Constant Expression using char
char character; cout << “Please select your menu”; cin >> character; //condition statement for switch (variable) switch(character){ case ‘A’: case ‘a’:{ //A & B known as constant expression cout << “You have select menu A”; break; } case ‘B’: case ‘b’:{//A & B known as constant expression cout << “You have select menu B”; default: cout << “Invalid selection”;

53 Advantages & Disadvantages
Suitable for menu selection. Proper code arrangement. Do not require the evaluation of logical expression. Disadvantage: Not suitable for comparing string (strcmp). Not suitable for comparing range of numbers.


Download ppt "Selection Control Structure"

Similar presentations


Ads by Google