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

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Chapter 4: Making Decisions.
CS0007: Introduction to Computer Programming
Selection (decision) control structure Learning objective
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 4: Making Decisions.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
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.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
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
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.
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
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
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.
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 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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.
Starting Out With Java 5 (Control Structures to Objects) Chapter 3 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
A First Book of C++ Chapter 4 Selection.
Chapter 4: Making Decisions.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Chapter 4: Making Decisions
Alternate Version of STARTING OUT WITH C++ 4th Edition
Alternate Version of STARTING OUT WITH C++ 4th Edition
Control Structures: Selection Statement
Topics 4.1 Relational Operators 4.2 The if Statement
Alternate Version of STARTING OUT WITH C++ 4th Edition
Understanding Conditions
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Control Structures: Selection Statement
Presentation transcript:

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

Chapter 4 slide 2 Topics 4.1 Relational Operators 4.2 The if Statement 4.3 The if/else Statement

Chapter 4 slide Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than or equal to <= Less than or equal to == Equal to != Not equal to

Chapter 4 slide 4 Relational Expressions Relational expressions are Boolean (i.e., evaluate to true or false) Examples: 12 > 5 is true 7 <= 5 is false if x is 10, then x == 10 is true, x != 8 is true, and x == 8 is false

Chapter 4 slide 5 Relational Expressions Can be assigned to a variable bool result = x <= y; Assigns 0 for false, 1 for true Do not confuse = and ==

Chapter 4 slide The if Statement Allows statements to be conditionally executed or skipped over Models the way we mentally evaluate situations “If it is cold outside, wear a coat and wear a hat.”

Chapter 4 slide 7 Format of the if Statement if (expression) { statement1; statement2; … statementn; } The block inside the braces is called the body of the if statement. If there is only 1 statement in the body, the { } may be omitted. No ; goes here ; goes here

Chapter 4 slide 8 How the if Statement Works If (expression) is true, then the statement(s) in the body are executed. If (expression) is false, then the statement(s) are skipped.

Chapter 4 slide 9 if Statement Flow of Control expression 1 or more statements true false

Chapter 4 slide 10 Example if Statements if (score >= 60) cout << “You passed.\n”; if (score >= 90) { grade = 'A'; cout << “Wonderful job!\n"; }

Chapter 4 slide 11 Flags Variables that signal conditions Usually implemented as a bool The flag value can be both set and tested with if statements

Chapter 4 slide 12 Flag Example Example: bool validMonths = true; … if (months < 0) validMonths = false; … if (validMonths) moPayment = total / months;

Chapter 4 slide The if/else Statement Allows a choice between statements depending on whether (expression) is true or false Format: if (expression) { statement set 1; } else { statement set 2; }

Chapter 4 slide 14 How the if/else Works If (expression) is true, statement set 1 is executed and statement set 2 is skipped. If (expression) is false, statement set 1 is skipped and statement set 2 is executed.

Chapter 4 slide 15 if/else Flow of Control expression statement set 1 true false statement set 2

Chapter 4 slide 16 Example if/else Statements if (score >= 60) cout << “You passed.\n”; else cout << “You did not pass.\n”; if (intRate > 0) { interest = loanAmt * intRate; cout << interest; } else cout << “You owe no interest.\n”;

Chapter 4 slide 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 18 if/else if Format if (expression) { statement set 1; } else if (expression) { statement set 2; } … else if (expression) { statement set n; }

Chapter 4 slide 19 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 20 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”;

Chapter 4 slide 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 22 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)

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