Download presentation
Presentation is loading. Please wait.
Published byShana Marshall Modified over 6 years ago
1
Decisions Given hours worked and pay rate, calculate total pay
What if you work overtime? How do you indicate if your work overtime? How about double overtime? COSC175-Selection
2
Relational Operators < less than <= less than or equal to
> greater than >= greater than or equal to == equals != not equal to COSC175-Selection
3
Examples 10 > 2 'a' < 'c' Why is this true? x != y
temp > humidity num == 35 initial != ‘Q’ COSC175-Selection
4
Logicals: AND && OR || NOT !
COSC175-Selection
5
Examples 1. if ((sex == 'F') && (city_code == 18) && (gpa >= 3.7))
cout << "Merit Award" << endl; 2. if ((zip == "48002") || (zip == "48003") || (zip == "48004")) cout << "LOCAL“ << endl; COSC175-Selection
6
“Short-Circuit” Evaluation
Process in which the computer evaluates a logical expression from left to right and stops as soon as the value of the expression is known one True in || -> True one False in && -> False Example: int age; int height; age = 25; height = 70; ((age > 50) && (height > 60)) 2. ((height > 60) || (age > 40))
7
Selection/Decision Test – true or false
One operation performed if true One operation performed if false Keywords if, else COSC175-Selection
8
if Syntax if ( Expression ) statement NOTE: statement can be
a single statement, a null statement, or a compound statement. COSC175-Selection
9
if statement is a selection
of whether or not to execute a statement (which can be a single statement or an entire block) TRUE expression FALSE statement COSC175-Selection
10
Examples if (taxCode == ‘T’ ) price = price + taxRate*Price;
2. if (num < 0 ) { // compound statement cout << "Negative" << endl; numNegs = numNegs + 1; } 3. if (age < 21 ) cout << "Under Age" COSC175-Selection
11
if-else Syntax NOTE: StatementA and StatementB each can be
if ( Expression ) StatementA else StatementB NOTE: StatementA and StatementB each can be a single statement a null statement, or a compound statement. COSC175-Selection
12
if..else provides two-way selection
between executing one of 2 clauses (the if clause or the else clause) TRUE FALSE expression if clause else clause COSC175-Selection
13
int carDoors, driverAge;
float premium, monthlyPayment; if ( (carDoors == 4 ) && (driverAge > 24) ) { premium = cout << "LOW RISK “ << endl; } else premium = cout << “ HIGH RISK ” << endl; monthlyPayment = premium / ; COSC175-Selection
14
Example Input Process Output temp Input temp, units newTemp units
Convert temperature from fahrenheit to celsius or from celsius to fahrenheit based on C or F Input Process Output temp Input temp, units newTemp units Convert temperature Output newTemp COSC175-Selection
15
code int main() { float temp; float newTemp; char units;
cout << " Enter temperature and C or F" cin >> temp >> units; if (units == 'F') newTemp = (5 * (temp – 32) )/9; cout << temp<< "Degrees F = " <<newTemp<<" Degrees C“; } else newTemp = ((9 * temp)/5) + 32; cout << "Degrees C = " << newTemp << " Degrees F“ << endl; } // end main COSC175-Selection
16
Exercises: Write the statements that allow you to input a number and prints whether it is positive or negative. Write the statements necessary to input the price of apples and the price of oranges. Then print either "THE APPLES COST MORE" or "THE ORANGES COST MORE. How would you handle the situation where the cost was the same? Write the statements that allow you to input a number and prints whether it is odd or even. COSC175-Selection
17
Nested selection - Example
if (hrsWorked <= 40 ) totPay = hrsWorked * payRate; else { regPay = 40 * payRate; if (hrsWorked <= 60) otPay = (hrsWorked – 40) * (1.5 * payRate); totPay = regPay + otPay; } otPay = 20 * (1.5 * payRate); doubleOtPay = (hrsWorked – 60) * (2 * payRate); grossPay = regPay + otPay + doubleOtPay; COSC175-Selection
18
Writing a nested if statement as a Multiple-Alternative Decision
if (x > 0) cout << “Positive” << endl; else if (x < 0) cout << “Negative” << endl; cout << “Zero” << endl; if (x > 0) cout << “Positive” << endl; else if (x < 0) cout << “Negative” << endl; else cout << “Zero” << endl; COSC175-Selection
19
Why is italicized code not necessary?
cin >> testGrade if (testGrade >= 90) letterGrade = ‘A‘; else if (testGrade >= 80) && testGrade < 90 letterGrade = 'B‘; else if (testGrade >= 70) && testGrade < 80 letterGrade = 'C‘; else if (testGrade >= 60) && testGrade < 70 letterGrade = 'D‘; else if testGrade < 60 letterGrade = 'F‘; Why is italicized code not necessary? COSC175-Selection
20
cin >> testGrade if (testGrade >= 90) letterGrade = 'A‘;
else if (testGrade >= 80 ) letterGrade = 'B‘; else if (testGrade >= 70 ) letterGrade = 'C‘; else if (testGrade >= 60 ) letterGrade = 'D‘; else letterGrade = 'F‘; COSC175-Selection
21
Nested if Statements if ( Expression1 ) Statement1
else if ( Expression2 ) Statement2 . else if ( ExpressionN ) StatementN else Statement N+1 EXACTLY 1 of these statements will be executed. COSC175-Selection
22
Multi-way Branching if ( creditsEarned >= 90 )
cout << “SENIOR STATUS “; else if ( creditsEarned >= 60 ) cout << “JUNIOR STATUS “; else if ( creditsEarned >= 30 ) cout << “SOPHOMORE STATUS “; else cout << “FRESHMAN STATUS “; COSC175-Selection
23
Use nested logic rather than straight thru logic
if (age < 16) charge = 7; if ((age >= 16) && (age < 65)) charge = 10; if (age >= 65) charge = 5; if (age < 16 ) charge = 7; else if ((age >= 16) && (age < 65)) charge = 10; else if (age >= 65 ) charge = 5; COSC175-Selection
24
Testing Selection Control Structures
to test a program with branches, use enough data sets so that every branch is executed at least once this is called minimum complete coverage COSC175-Selection
25
Dangling else else matches nearest unmatched if
if (animal == “dog”) if (weight > 40) cost = 15; else cost = 10; if (animal == “dog”) if (weight > 40) cost = 15; else cost = 10; COSC175-Selection
26
switch Use for multiple tests of one variable switch (selector) {
case label1: statement 1 case label2: statement 2 .. case label n: statement n default: statement e } Evaluation: The selector expression is evaluated and compared to each of the case labels. Each labeli is a list of one or more possible constants, separated by commas. Only one statement will be executed. Control is then passed to the first statement following the end of the case. Each statement may be a single or compound statement. COSC175-Selection
27
Example cin >> menuOpt; switch (menuOpt) { case 1:
cout << "Beginners“; break; case 2: cout << "Advanced Beginner“; case 3: cout << "Intermediate“; default: cout << "Invalid Option“ } //END CASE COSC175-Selection
28
Example cin >> menuOpt; switch (menuOpt) { case 1: case 2:
cout << “Buckle my shoe“; break; case 3: case 4: cout << “Shut the door“; case 5: case 6: cout << “Pick up stickse“; default: cout << "Invalid Option“ } //END CASE COSC175-Selection
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.