COSC175-Selection1 Decisions Given hours worked and pay rate, calculate total pay What if you work overtime? How do you indicate if your work overtime?

Slides:



Advertisements
Similar presentations
Chapter 4: Making Decisions.
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
1 Chapter 5 Branching and Method Algorithm Design.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Use Precedence Chart int number ; float x ; number ! = 0 && x < 1 / number / has highest priority < next priority != next priority && next priority What.
Multi-alternative Selection Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection.
Single selection syntax if ( expression ) { statements; } TRUE FALSE expression if clause 1. Statemens are executed when expression is true 2. { } can.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems/Headington.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems.
Control Structures I (Selection)
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures.
Control Structures special statements whose purpose is to change the order of execution in a program. Sequential structures – Statements executed sequentially.
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures.
Conditions, Logical Expressions, and Selection Control Structures Sumber dari :
Chapter 4 Logical Expressions & If-Else. 2 Overview  More on Data Type bool u Using Relational & Logical Operators to Construct & Evaluate Logical Expressions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures I
CPS120: Introduction to Computer Science Decision Making in Programs.
CONTROL STRUCTURES (MULTI-WAY SELECTION). MULTI-WAY SELECTION  EXTENDED IF-ELSE  Used to select exactly one task out of multiple tasks (or possibly.
Selection Structures (if & switch statements) (CS1123)
Chapter 4 Selection Structures: Making Decisions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Chapter 3 More Flow Of Control.
CONTROLLING PROGRAM FLOW
1 Chapter 9 Additional Control Structures Dale/Weems.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Control Structures 1. Control Structures Java Programming: From Problem Analysis to Program Design, D.S. Malik 2.
Chapter 4: Control Structures SELECTION STATEMENTS.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapter 4 Selection: the if-else and switch-case instructions.
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems.
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
1 CS161 Introduction to Computer Science Topic #8.
Week 4 Program Control Structure
1 Conditions, Logical Expressions, and Selection Control Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Selection in C++ If statements. Control Structures Sequence Selection Repetition Module.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
C++ Programming Control Structures I (Selection).
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
A First Book of C++ Chapter 4 Selection.
Chapter 3 Selection Statements
Chapter 4: Control Structures I
Chapter 4: Control Structures I
Decisions Given hours worked and pay rate, calculate total pay
Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point.
Decisions Given hours worked and pay rate, calculate total pay
Chapter 4: Control Structures I
Flow Control Statements
Structured Program Development in C++
Control Statements Paritosh Srivastava.
Presentation transcript:

COSC175-Selection1 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-Selection2 Relational Operators < less than <= less than or equal to > greater than >= greater than or equal to == equals != not equal to

Examples COSC175-Selection > 2 2.'a' < 'c' Why is this true? 3.x != y 4.temp > humidity 5.num == 35 6.initial != ‘Q’

COSC175-Selection4 Logicals: AND && OR || NOT !

COSC175-Selection5 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;

“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; 1.((age > 50) && (height > 60)) 2. ((height > 60) || (age > 40))

COSC175-Selection7 Selection/Decision Test – true or false One operation performed if true One operation performed if false Keywords –if, else

COSC175-Selection8 if ( Expression ) statement NOTE: statement can be a single statement, a null statement, or a compound statement. if Syntax

COSC175-Selection9 if statement is a selection of whether or not to execute a statement (which can be a single statement or an entire block ) TRUE FALSE statement expression

COSC175-Selection10 Examples 1.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-Selection11 if ( Expression ) StatementA else StatementB NOTE: StatementA and StatementB each can be a single statement a null statement, or a compound statement. if-else Syntax

COSC175-Selection12 if..else provides two-way selection between executing one of 2 clauses (the if clause or the else clause) TRUE FALSE if clauseelse clause expression

COSC175-Selection13 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-Selection14 Example InputProcessOutput tempInput temp, unitsnewTemp unitsConvert temperature Output newTemp Convert temperature from fahrenheit to celsius or from celsius to fahrenheit based on C or F

COSC175-Selection15 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-Selection16 Exercises: 1.Write the statements that allow you to input a number and prints whether it is positive or negative. 2.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? 3.Write the statements that allow you to input a number and prints whether it is odd or even.

COSC175-Selection17 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; } else { otPay = 20 * (1.5 * payRate); doubleOtPay = (hrsWorked – 60) * (2 * payRate); grossPay = regPay + otPay + doubleOtPay; }

COSC175-Selection18 Writing a nested if statement as a Multiple-Alternative Decision if (x > 0) cout << “Positive” << endl; else if (x < 0) cout << “Negative” << endl; else cout << “Zero” << endl; if (x > 0) cout << “Positive” << endl; else if (x < 0) cout << “Negative” << endl; else cout << “Zero” << endl;

COSC175-Selection19 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-Selection20 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-Selection21 if ( Expression1 ) Statement1 else if ( Expression2 ) Statement2. else if ( ExpressionN ) StatementN else Statement N+1 EXACTLY 1 of these statements will be executed. Nested if Statements

COSC175-Selection22 if ( creditsEarned >= 90 ) cout << “SENIOR STATUS “; else if ( creditsEarned >= 60 ) cout << “JUNIOR STATUS “; else if ( creditsEarned >= 30 ) cout << “SOPHOMORE STATUS “; else cout << “FRESHMAN STATUS “; Multi-way Branching

COSC175-Selection23 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-Selection24 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-Selection25 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 }

COSC175-Selection26 Example cin >> menuOpt; switch (menuOpt) { case 1: cout << "Beginners“; break; case 2: cout << "Advanced Beginner“; break; case 3: cout << "Intermediate“; break; default: cout << "Invalid Option“ } //END CASE

COSC175-Selection27 Example cin >> menuOpt; switch (menuOpt) { case 1: case 2: cout << “Buckle my shoe“; break; case 3: case 4: cout << “Shut the door“; break; case 5: case 6: cout << “Pick up stickse“; break; default: cout << "Invalid Option“ } //END CASE