An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure.

Slides:



Advertisements
Similar presentations
1.
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Programming with Microsoft Visual Basic th Edition
Objectives AND logic OR logic Evaluating compound conditions with multiple logical operators Precedence when combining AND and OR operators Efficiency.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic.NET, Second Edition.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
An Introduction to Programming with C++ Fifth Edition
An Introduction to Programming with C++ Fifth Edition Chapter 12 String Manipulation.
An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions.
An Introduction to Programming with C++ Fifth Edition
An Introduction to Programming with C++ Fifth Edition Chapter 13 Sequential Access Files.
An Introduction to Programming with C++ Fifth Edition
An Introduction to Programming with C++ Fifth Edition Chapter 6 More on the Selection Structure.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
An Introduction to Programming with C++ Fifth Edition Chapter 4 Chapter 4: Variables, Constants, and Arithmetic Operators.
An Introduction to Programming with C++ Fifth Edition Chapter 8 More on the Repetition Structure.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
Chapter Four The Selection Structure
CIS162AD - C# Decision Statements 04_decisions.ppt.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
Programming Logic and Design Sixth Edition
Using the selection structure (Unit 7) Visual Basic for Applications.
Chapter 4: The Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter 4: The Selection Structure
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second.
Selection Structure If... Then.. Else Case. Selection Structure Use to make a decision or comparison and then, based on the result of that decision or.
Chapter 4 Selection Structures: Making Decisions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Chapter Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
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.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
110 F-1 Decisions and Conditions Chapter 4: We can design a form We can calculate To make our programs more powerful, we need to be able to make choices.
Tutorial 4: The Selection Structure 1 Tutorial 4 The Selection Structure.
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Chapter 10 So Many Paths … So Little Time (Multiple-Alternative Selection Structures) Clearly Visual Basic: Programming with Visual Basic nd Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
An Introduction to Programming with C++1 The Selection Structure Tutorial 6.
Random Functions Selection Structure Comparison Operators Logical Operator
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.
Exam 1 Review Jeff has 100 students in his MIS120 class. He is giving a 50 question exam worth 100 points. The following commands are available to you.
Chapter 4: Control Structures I
More on the Selection Structure
The Selection Structure
1.
Chapter 4: The Selection Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
An Introduction to Programming with C++ Fifth Edition
Microsoft Visual Basic 2005 BASICS
Making Decisions in a Program
Objectives After studying this chapter, you should be able to:
Chapter 8: More on the Repetition Structure
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
An Introduction to Programming with C++ Fifth Edition
Chapter 3: Selection Structures: Making Decisions
The Selection Structure
Chapter 5: The Selection Structure
Presentation transcript:

An Introduction to Programming with C++ Fifth Edition Chapter 5 The Selection Structure

An Introduction to Programming with C++, Fifth Edition2 Objectives Write pseudocode for the selection structure Create a flowchart for the selection structure Code the if and if/else forms of the selection structure Write code that uses comparison operators and logical operators

An Introduction to Programming with C++, Fifth Edition3 Objectives (continued) Convert the contents of a char variable to uppercase or lowercase Convert the contents of a string variable to uppercase or lowercase Use the.NET ToUpper(), ToLower(), and CompareTo() methods Format numeric output in.NET

An Introduction to Programming with C++, Fifth Edition4 Concept Lesson Using the Selection Structure Writing Pseudocode for the if and if/else Selection Structures Flowcharting the if and if/else Selection Structures Coding the if and if/else Selection Structures

An Introduction to Programming with C++, Fifth Edition5 Concept Lesson (continued) Comparison Operators Logical Operators Comparing Characters Comparing Strings

An Introduction to Programming with C++, Fifth Edition6 Using the Selection Structure Also called the decision structure –Condition specifies decision Results in either a true or false answer only –Three forms: if, if/else, and switch (or case )

Using the Selection Structure (continued) An Introduction to Programming with C++, Fifth Edition7

8 Writing Pseudocode for the if and if/else Selection Structures

An Introduction to Programming with C++, Fifth Edition9 Flowcharting the if and if/else Selection Structures selection/repetition symbol

An Introduction to Programming with C++, Fifth Edition10 Coding the if and if/else Selection Structures

An Introduction to Programming with C++, Fifth Edition11 Coding the if and if/else Selection Structures (continued)

An Introduction to Programming with C++, Fifth Edition12 Comparison Operators Often called relational operators If expression has multiple comparison operators at same precedence, it is evaluated from left to right Comparison operators are evaluated after any arithmetic operators in the expression

Comparison Operators (continued) An Introduction to Programming with C++, Fifth Edition13

An Introduction to Programming with C++, Fifth Edition14 Comparison Operators (continued)

An Introduction to Programming with C++, Fifth Edition15 Comparison Operator Program 1: Swapping Numerical Values

An Introduction to Programming with C++, Fifth Edition16 Comparison Operator Program 1: Swapping Numerical Values (continued)

An Introduction to Programming with C++, Fifth Edition17 temp is a local variable Comparison Operator Program 1: Swapping Numerical Values (continued)

An Introduction to Programming with C++, Fifth Edition18 Comparison Operator Program 1: Swapping Numerical Values (continued)

An Introduction to Programming with C++, Fifth Edition19

An Introduction to Programming with C++, Fifth Edition20 Comparison Operator Program 2: Displaying the Sum or Difference

An Introduction to Programming with C++, Fifth Edition21 Comparison Operator Program 2: Displaying the Sum or Difference (continued)

An Introduction to Programming with C++, Fifth Edition22 Comparison Operator Program 2: Displaying the Sum or Difference (continued)

An Introduction to Programming with C++, Fifth Edition23 Comparison Operator Program 2: Displaying the Sum or Difference (continued)

An Introduction to Programming with C++, Fifth Edition24 Logical Operators Logical operators allow you to combine two or more conditions into one compound condition –Sometimes called Boolean operators And/Or operators are evaluated after any arithmetic or comparison operators in an expression

Logical Operators (continued) An Introduction to Programming with C++, Fifth Edition25

An Introduction to Programming with C++, Fifth Edition26 Logical Operators (continued) use short-circuit evaluation

An Introduction to Programming with C++, Fifth Edition27 Using the Truth Tables To receive a bonus, a salesperson must be rated A and he/she must sell more than $10,000 in product rating == 'A' && sales > To send a letter to all A-rated salespeople and all B-rated salespeople rating == 'A' || rating == 'B'

An Introduction to Programming with C++, Fifth Edition28 Logical Operators (continued)

An Introduction to Programming with C++, Fifth Edition29 Logical Operators (continued)

An Introduction to Programming with C++, Fifth Edition30 Logical Operator Program: Calculating Gross Pay

An Introduction to Programming with C++, Fifth Edition31 data validation Logical Operator Program: Calculating Gross Pay (continued)

An Introduction to Programming with C++, Fifth Edition32 Logical Operator Program: Calculating Gross Pay (continued)

An Introduction to Programming with C++, Fifth Edition33 Comparing Characters Display the word “Pass” if user enters the letter P (uppercase or lowercase) Display “Fail” if user enters anything else

An Introduction to Programming with C++, Fifth Edition34 Comparing Characters (continued)

An Introduction to Programming with C++, Fifth Edition35 Comparing Characters (continued)

An Introduction to Programming with C++, Fifth Edition36 Converting a Character to Uppercase or Lowercase

An Introduction to Programming with C++, Fifth Edition37 Comparing Strings String comparisons are case-sensitive –“yes”, “YES”, and “Yes” are different Before using a string in a comparison, convert it to uppercase or lowercase –Use transform()

An Introduction to Programming with C++, Fifth Edition38 Converting a String to Uppercase or Lowercase #include using std::transform;

An Introduction to Programming with C++, Fifth Edition39 transform() Function Program: Calculating Sales Tax Calculate and display the amount of sales tax that a customer owes Tax rate is 3% of purchase price If purchase is made in Gunter County, tax rate is 3.25%

An Introduction to Programming with C++, Fifth Edition40 transform() Function Program: Calculating Sales Tax (continued)

An Introduction to Programming with C++, Fifth Edition41 transform() Function Program: Calculating Sales Tax (continued)

An Introduction to Programming with C++, Fifth Edition42 Summary The selection structure (decision structure) is one of the three programming structures –Forms: if, if/else, and switch (or case ) –Represented by a diamond in a flowchart –Use the C++ if statement to code both the if and if/else forms Condition can contain variables, constants, functions, and arithmetic, comparison, or logical operators Character and string comparisons are case sensitive –Use toupper(), tolower() and transform()

An Introduction to Programming with C++, Fifth Edition43 Application Lesson: Using the Selection Structure in a C++ Program Lab 5.1: Stop and Analyze Lab 5.2 –Create the Willow Springs Health Club program Lab 5.3 –Modify program so that the if statement’s true path handles the invalid data, and its false path handles the valid data Lab 5.4: Desk-Check Lab Lab 5.5: Debugging Lab