Alternate Version of STARTING OUT WITH C++ 4th Edition

Slides:



Advertisements
Similar presentations
Selection (decision) control structure Learning objective
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 4: Making Decisions.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 4 Making Decisions.
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 4 Making.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions Continued.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Other Conditional Methods. Conditional Operator Conditional Operator (?:) Conditional operator ( ?: ) – Ternary operator: 3 arguments expression1 ? expression2.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 4: Making Decisions. Resource: Starting Out with C++, Third Edition, Tony Gaddis 4.1 Relational Operators Relational operators allow you to compare.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Chapter 4: Making Decisions Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda CMPS 1043 – Computer.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Lecture 8: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions Starting Out with C++ Early Objects Seventh Edition.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions Starting Out with C++ Early Objects Eighth.
+ Chapter 4: Making Decisions Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions Starting Out with C++ Early Objects Seventh Edition.
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.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Chapter 3 Selection Statements
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
CMPT 201 if-else statement
The Selection Structure
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
An Introduction to Programming with C++ Fifth Edition
Control Structures – Selection
Chapter 4: Making Decisions
Topics The if Statement The if-else Statement Comparing Strings
Other Conditional Methods
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 4: Decision Structures and Boolean Logic
Chapter 7 Conditional Statements
Topics 4.1 Relational Operators 4.2 The if Statement
Alternate Version of STARTING OUT WITH C++ 4th Edition
Alternate Version of STARTING OUT WITH C++ 4th Edition
Boolean Expressions to Make Comparisons
Chapter 4: Decision Structures and Boolean Logic
Presentation transcript:

Alternate Version of STARTING OUT WITH C++ 4th Edition Chapter 4 Making Decisions Copyright 2004 Scott/Jones Publishing

Topics 4.4 The if/else if Statement 4.5 Menus 4.6 Nested if Statements 4.7 Logical Operators 4.8 Checking Numeric Ranges with Logical Operators Chapter 4 slide 2

4.4 The if/else if Statement Chain of if statements that test in order until one is found to be true Also models thought processes “If it is raining, take an umbrella, else, if it is windy, take a hat, else, if it is sunny, take sunglasses.” Chapter 4 slide 3

if/else if Format if (expression) { statement set 1; } else if (expression) { statement set 2; … { statement set n; See pr4-07.cpp Chapter 4 slide 4

Using a Trailing else Used with if/else if statement when none of (expression) is true Provides a default statement/action Can be used to catch invalid values or handle other exceptional situations Chapter 4 slide 5

Example if/else if with Trailing else if (age >= 21) cout << “Adult”; else if (age >= 13) cout << “Teen”; else if (age >= 2) cout << “Child”; else cout << “Baby”; See pr4-09.cpp Chapter 4 slide 6

4.5 Menus Menu-driven program: program execution controlled by user selecting from a list of actions Menu: list of choices on the screen Can be implemented using if/else if statements Chapter 4 slide 7

Menu-driven program organization Display list of numbered or lettered choices for actions. Input user’s selection Test user selection in (expression) if a match, then execute code to carry out desired action if not, then test with next (expression) See pr4-10.cpp Chapter 4 slide 8

4.6 Nested if Statements An if statement that is part of the if or else part of another if statement Can be used to evaluate > 1 data item or condition if (score < 100) { if (score > 90) grade = 'A'; } Chapter 4 slide 9

Notes on Coding Nested ifs An else matches the nearest if that does not have an else if (score < 100) if (score > 90) grade = 'A'; else ... // goes with second if, // not first one Proper indentation helps greatly See pr4-11.cpp and pr4-12.cpp Chapter 4 slide 10

Operators, Meaning, and Explanation 4.7 Logical Operators Used to create relational expressions from other relational expressions Operators, Meaning, and Explanation && AND New relational expression is true if both expressions are true || OR New relational expression is true if either expression is true ! NOT Reverses the value of an expression; true expression becomes false, and false becomes true Chapter 4 slide 11

Logical Operator Examples int x = 12, y = 5, z = -4; (x > y) && (y > z) true (x > y) && (z > y) false (x <= z) || (y == z) (x <= z) || (y != z) !(x >= z) See pr4-13.cpp, pr4-14.cpp, and pr4-15.cpp Chapter 4 slide 12

Logical Precedence Example: (2 < 3) || (5 > 6) && (7 > 8) Highest ! && Lowest || Example: (2 < 3) || (5 > 6) && (7 > 8) is true because AND is done before OR Chapter 4 slide 13

More on Precedence Highest arithmetic operators relational operators Lowest logical operators Example: 8 < 2 + 7 || 5 == 6 is true Chapter 4 slide 14

Logical Operator Notes Short circuit evaluation If the value of an expression can be determined by evaluating just the sub-expression on left side of a logical operator, the sub-expression on the right side is not evaluated True OR anything is true False AND anything is false Chapter 4 slide 15

4.8 Checking Numeric Ranges with Logical Operators Used to test if a value is within a range if (grade >= 0 && grade <= 100) cout << "Valid grade"; Can also test if a value lies outside a range if (grade <= 0 || grade >= 100) cout << "Invalid grade"; Cannot use mathematical notation if (0 <= grade <= 100) //Doesn’t //work! Chapter 4 slide 16

Alternate Version of STARTING OUT WITH C++ 4th Edition Chapter 4 Making Decisions Copyright 2004 Scott/Jones Publishing