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

2 2) Relational Operators 1) Boolean values
3) Logical Operators 4) Type of selection control structures

3 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;

4 Relational operators

5 Relational operator Relational operators are used to compare two values to form a condition. Math C++ Plain English = == equals [example: if(a==b) ] [ (a=b) means put the value of b into a ] < < less than  <= less than or equal to > > greater than  >= greater than or equal to  != not equal to

6 Relational operator Relational operators:
Allow comparisons Require two operands (binary) Return 1 if expression is true, 0 otherwise. Comparing values of different data types may produce unpredictable results For example, 8 < '5' should not be done.

7 Simple Boolean Expressions
Example: 8 < 15 = true 2.5 >= 5.8 = false Exercise: 6 != 6 5.9 <= 7.5 False True

8 Logical operators

9 Logical operator OPERATOR DESCRIPTION ! Not && AND || OR The AND operator will give the value TRUE when all expression are TRUE The OR operator will give the value TRUE if any expression is TRUE Putting ! in front of a logical expression reverses its value

10 logical operator Truth table 1 1 1 1

11 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 =

12 Logical expression

13 Logical expression Expression that can be thought of as being true or false. True if correct or false if not correct. The expression uses relational operators such as == and < and logical operators such as &&, ||, and !

14 Logical expression Logical expressions can be unpredictable
The following expression appears to represent a comparison of 0, num = 12, and 10: <= num <= 10 It always evaluates true because 0<=12 evaluates to 1, and 1 <= 10 is true. A correct way to write this expression is: 0 <= num && num <= 10

15 Logical expression Example: (true || false) (7>3 && 5<2)

16 Exercises x<y && z>y x<=z && x>0 x>=y && z==3
Assume x =5, y = 6 and z =7 Boolean expression Value x<y && z>y x<=z && x>0 x>=y && z==3 y<15||!(y>0) x>y||z>y && !(z<=0) 11011

17 Exercises Evaluate the following expression: (17 < 4 * 3 + 5) || (8 * 2 == 4 * 4) && !(3 + 3 == 6) = (17 < ) || (16 == 16) && !(6 == 6) = (17 < 17 ) || true && !(true) = false || true && false = false || false = false = 0

18 Decision control structure

19 Decision control instruction
You use the decision control structure or selection control sturcture when you want program to make a decision or comparison and then select one of two paths, depending on the result of that decision or comparison. Example 1 Example 2 If (it is raining) wear a rain coat bring an umbrella If (you have a test tomorrow) study tonight Else watch a movie

20 Decision control structure
1 One way selection 2 Two way selection 3 Multiple selection

21 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

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

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

24 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

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

26 Exercises 1 #include <iostream.h> void main() { int mark; cin >> mark; if (mark >= 50) cout << “PASSED!”; } Ask student to do pseudocode and flowchart also What is the output if I enter 45???

27 Exercises 2 #include <iostream.h> void main() { int mark; cin >> mark; if (mark >= 50) cout << “PASSED!”; cout<< “thank you for using the program”<<endl; } Ask student to do pseudocode and flowchart also What is the output if I enter 45 and then 50???

28 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.

29 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

30 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

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

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

33 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

34 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

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

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

37 Compound statements

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

39 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”; }

40 Exercise Assume that a program need to determine whether a student’s gender is male or female. If student’s gender equal to ‘f’ then display female; otherwise display male. Write pseudocode for the selection structure. Write a C++ if statement that corresponds to the pseudocode in question 1.

41 Exercise (cont.) Problem statement:
A shopkeeper sells a magazine for RM2.00 each. However, if the customer buys more than 10 magazines, the price of the magazine is only RM1.50 each. Calculate the total the customer has to pay based on the quantity that he buys.

42 Total = quantity * price
Start Input quantity T Display total F quantity>10 price = 2.00 price = 1.50 Total = quantity * price Begin input quantity if quantity > 10 then price = 1.50 else price = 2.00 total = price * quantity output total End End

43 Syntax and code #include <iostream.h> void main() {
int quantity = 0; float total = 0; cin >> quantity; if (quantity > 10) price = 1.50; else price = 2.00 total = quantity * price; cout << total; }

44 Exercise (one way VS two way)
Determine the output of both program segment: A group of one way selection structure: Two way selection structure: int x = -3; if(x<10) x = * x; If(x<100) x = x + 10; cout<<x; int x = -3; if(x<10) x = * x; else x = x + 10; cout<<x;

45 Thank You !


Download ppt "Selection Control Structure"

Similar presentations


Ads by Google