Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.

Slides:



Advertisements
Similar presentations
1.
Advertisements

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Chapter 5– Making Decisions Dr. Jim Burns. In Java, the simplest statement.. Is the if(..) statement if(someVariable==10) System.out.println(“The value.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
Objectives AND logic OR logic Evaluating compound conditions with multiple logical operators Precedence when combining AND and OR operators Efficiency.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
An Introduction to Programming with C++ Fifth Edition Chapter 6 More on the Selection Structure.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
C++ for Engineers and Scientists Third Edition
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Programming Logic and Design Fourth Edition, Introductory
Programming Logic and Design Sixth Edition
Chapter 3 Making Decisions
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
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)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Fundamental Programming Fundamental Programming More on Selection.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Chapter 4 Selection Structures: Making Decisions.
Programming Logic and Design, Second Edition, Comprehensive
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
CHAPTER 4: Selection Control Structure. Objectives  Use the relational comparison operators  Learn about AND logic  Learn about OR logic  Make selections.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Copyright 2003 Scott/Jones Publishing Making Decisions.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Chapter 5: Making Decisions
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Decision Statements, Short- Circuit Evaluation, Errors.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 4 Making Decisions.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Java Programming Fifth Edition
More on the Selection Structure
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 4: Control Structures I (Selection)
Chapter 8: More on the Repetition Structure
Chapter 4 Selection.
Chapter 4: Control Structures I (Selection)
Boolean Expressions to Make Comparisons
The Selection Structure
Using C++ Arithmetic Operators and Control Structures
Presentation transcript:

Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions

Object-Oriented Programming Using C++, Third Edition2 Objectives Use the if and if-else statements Use nested if statements Avoid common pitfalls with if statements Use the switch statement Use the conditional operator Use the logical AND and the logical OR Make decisions with structure fields

Object-Oriented Programming Using C++, Third Edition3 Using the if Statement The primary C++ selection structure statement used to perform a single-alternative selection is an if statement if (Boolean expression) action if true; There is also a dual-alternative if if (Boolean expression) action if true; else action if false;

Object-Oriented Programming Using C++, Third Edition4 Using the if Statement (continued)

Object-Oriented Programming Using C++, Third Edition5 Using the if Statement (continued)

Object-Oriented Programming Using C++, Third Edition6 Using the if Statement (continued)

Object-Oriented Programming Using C++, Third Edition7 Using the if Statement (continued) Performing multiple tasks when a condition is met

Object-Oriented Programming Using C++, Third Edition8 Using the if Statement (continued) Don’t forget the curly braces

Object-Oriented Programming Using C++, Third Edition9 The Dual-Alternative if Also called the if-else structure

Object-Oriented Programming Using C++, Third Edition10 Using a Nested if Nested if : if structure that rests entirely within another if structure—within either the if or the else clause Consider the code in the previous slide –If the user enters ‘T’, it will display “Male” –You can use a nested if to make the code more sophisticated

Object-Oriented Programming Using C++, Third Edition11 Also called an if-else-if structure Using a Nested if (continued)

Object-Oriented Programming Using C++, Third Edition12 Avoiding Common Pitfalls with if Statements Forgetting that C++ is case sensitive Assuming that indentation has a logical purpose Adding an unwanted semicolon Forgetting curly braces Using = instead of == Making unnecessary comparisons Creating unreachable code See Figure 3-6

Object-Oriented Programming Using C++, Third Edition13 Pitfall: Forgetting that C++ is Case Sensitive Code in Figure 3-10 displays “Invalid code” if a user enters ‘f’

Object-Oriented Programming Using C++, Third Edition14 Pitfall: Adding an Unwanted Semicolon Indentation has no logical purpose

Object-Oriented Programming Using C++, Third Edition15 Pitfall: Using = instead of ==

Object-Oriented Programming Using C++, Third Edition16 Pitfall: Making Unnecessary Comparisons

Object-Oriented Programming Using C++, Third Edition17 Pitfall: Making Unnecessary Comparisons (continued)

Object-Oriented Programming Using C++, Third Edition18 Pitfall: Creating Unreachable Code

Object-Oriented Programming Using C++, Third Edition19 Pitfall: Creating Unreachable Code (continued)

Object-Oriented Programming Using C++, Third Edition20 Using the switch Statement When you want to create different outcomes depending on specific values, you can use a series of if s

Object-Oriented Programming Using C++, Third Edition21 Using the switch Statement (continued) However, as an alternative to the long string of if s, you can use the switch statement Removing break changes behavior of statement

Object-Oriented Programming Using C++, Third Edition22 Using the Conditional Operator The conditional operator is represented by a question mark Provides a concise way to express two alternatives –Alternative one: if(driverAge < 26) insurancePremium = 250; else insurancePremium = 185; –Alternative two: driverAge < 26 ? insurancePremium = 250 : insurancePremium = 185; cout b ? a : b)<<“ is greater”<<endl;

Object-Oriented Programming Using C++, Third Edition23 Using the Logical AND Consider the following example

Object-Oriented Programming Using C++, Third Edition24 Using the Logical AND (continued) You can use a single if statement that contains a logical AND, which you create by typing two ampersands (&&) between two Boolean expressions: Remember to include a complete Boolean expression on each side of the && if(payRate >= 6.00 && <= 12.00) // ERROR! cout<<"Valid "<<endl;

Object-Oriented Programming Using C++, Third Edition25 Using the Logical AND (continued)

Object-Oriented Programming Using C++, Third Edition26 Using the Logical OR Consider the following example

Object-Oriented Programming Using C++, Third Edition27 Using the Logical OR (continued) You can use a single if statement that contains a logical OR, which you create by typing two pipes (||) between two Boolean expressions

Object-Oriented Programming Using C++, Third Edition28 Using the Logical OR (continued)

Object-Oriented Programming Using C++, Third Edition29 Pitfall: Using OR when you mean AND Write a program given the following instructions: “The user should enter 1 or 2. If the user doesn’t enter 1 or 2, issue an error message.” Incorrect: if(userResponse != 1 || userResponse != 2) cout<<"Incorrect entry"<<endl; Correct: if(userResponse != 1 && userResponse != 2) cout<<"Incorrect entry"<<endl;

Object-Oriented Programming Using C++, Third Edition30 Combining AND and OR Selections AND takes precedence over OR –AND is evaluated first

Object-Oriented Programming Using C++, Third Edition31 Making Decisions with Structure Fields

Object-Oriented Programming Using C++, Third Edition32

Object-Oriented Programming Using C++, Third Edition33 You Do It: Using a Single-Alternative if cout<<"On average, how many hours are you gone from home each day? "; cin>>hoursGone; if(hoursGone > MANY_HOURS) cout<<"You should consider a cat"<<endl;

Object-Oriented Programming Using C++, Third Edition34 Using a Dual-Alternative if else cout<<"You can consider a dog"<<endl;

Object-Oriented Programming Using C++, Third Edition35 Using a Compound Condition and Nested if s … …

Object-Oriented Programming Using C++, Third Edition36 Summary if statement is the primary selection structure statement to perform a single-alternative selection Dual-alternative if takes one action when its Boolean expression is evaluated as true, and uses an else clause to define the actions to take when the expression is evaluated as false A nested if rests entirely within another if structure Easy to make several types of mistakes

Object-Oriented Programming Using C++, Third Edition37 Summary (continued) Use a series of if s or use the switch statement when you want to create different outcomes depending on specific values of a variable Conditional operator (?) provides a concise way to express two alternatives The logical AND is created by typing two ampersands (&&) Use a logical AND to create a compound Boolean expression in which two conditions must be true for the entire expression to evaluate as true

Object-Oriented Programming Using C++, Third Edition38 Summary (continued) Logical OR is created by typing two pipes (||) Use a logical OR to create a compound Boolean expression in which at least one of two conditions must be true for the expression to evaluate as true When you combine an AND and an OR in the same expression, the AND takes precedence When you define a structure, and subsequently create objects that are instantiations of the structure, you use the individual structure’s fields the same way you use variables of the same type