Chapter 4: Making Decisions. Resource: Starting Out with C++, Third Edition, Tony Gaddis 4.1 Relational Operators Relational operators allow you to compare.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 4: Making Decisions.
Advertisements

Chapter 4. Making Decisions
Starting Out with C++, 3 rd Edition 1 Chapter 5. Looping.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
The If/Else Statement, Boolean Flags, and Menus Page 180
C++ Programming, Namiq Sultan1 Chapter 4 Making Decisions Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:
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.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions Continued.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Chapter 4 Selection Structures: Making Decisions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
Starting Out with C++: From Control Structures through Objects 7 th edition By Tony Gaddis Source Code Chapter 4.
CONTROLLING PROGRAM FLOW
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.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
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.
+ Chapter 4: Making Decisions Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Chapter Making Decisions 4. Relational Operators 4.1.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
Lecture 9: Making Decisions Final Section Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Chapter#3 Part1 Structured Program Development in C++
1 CS161 Introduction to Computer Science Topic #8.
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.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
A First Book of C++ Chapter 4 Selection.
Chapter 4. Making Decisions
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 7 Conditional Statements
Presentation transcript:

Chapter 4: Making Decisions

Resource: Starting Out with C++, Third Edition, Tony Gaddis 4.1 Relational Operators Relational operators allow you to compare numeric values and determine if one is greater than, less than, equal to, or not equal to another. Chars are also considered numeric values.

Resource: Starting Out with C++, Third Edition, Tony Gaddis Programs You’ve Written Gather input from the user. Perform one or more calculations. Display the results on the screen.

Resource: Starting Out with C++, Third Edition, Tony Gaddis Computers Good at performing calculations. Adept to comparing values to determine if one is greater than, less than, or equal to, the other. The computer can assist with: –Examining sales figures –Determining profit and loss –Checking a number to ensure it is within an acceptable range –Validating the input given by the user

Table 4-3: Relational Expressions and Their True or False Values In C++, relational expressions represent true states with the number 1 and false states with the number 0.

Table 4-5: Examples of if Statements Here is the general format of the if statement: If (expression) statement;

Resource: Starting Out with C++, Third Edition, Tony Gaddis Be Careful With Semicolons Semicolons do not mark the end of a line, but the end of a complete C++ statement. The if statement isn’t complete without the conditionally executed statement that comes after it. Therefore, you do not put a semicolon after the if (expression) portion of an if statement. if (expression)No semicolon goes here statement;Semicolon goes here

Resource: Starting Out with C++, Third Edition, Tony Gaddis Program 4-3 //This program demonstrates how a misplaced semicolon //prematurely terminates an if statement. #include void main(void) { int x = 0, y = 10; cout << “x is " << x << " and y is " << y << endl; if (x > y); // misplaced semicolon! cout << “x is greater than y\n"; // Always executed } Program Output X is 0 and Y is 10 X is greater than Y

Resource: Starting Out with C++, Third Edition, Tony Gaddis Comparing Floating Point Numbers You should be careful when using the equality operator (==) to compare floating point values. Because of round-off errors. A number that should be mathematically equal to another might not be. To prevent errors like this, stick with greater-than and less-than comparisons with floating point numbers. Refer to Program 4-4

Resource: Starting Out with C++, Third Edition, Tony Gaddis 4.4 Expanding the if Statement The if statement can conditionally execute a block of statement enclosed in braces. if (expression) { statement; // Place as many statements here as necessary. }

Resource: Starting Out with C++, Third Edition, Tony Gaddis Program 4-7 Continued cout.precision(1); cout.setf(ios::showpoint | ios::fixed); cout << "Your average is " << average << endl; if (highScore) { cout << "Congratulations!\n"; cout << "That's a high score.\n"; cout << "You deserve a pat on the back!\n"; }

Resource: Starting Out with C++, Third Edition, Tony Gaddis Don’t Forget the Braces! If you intend to execute a block of statements with an if statement, don’t forget the braces. Without the braces, the if statement only executes the very next statement.

Resource: Starting Out with C++, Third Edition, Tony Gaddis 4.5 The if/else Statement The if/else statement will execute one group of statements if the expression is true, or another group of statements if the expression is false. The if/else statement is an expansion of the if statement. Here is the format: if (expression) statement or block of statements; else statement or block of statements;

Resource: Starting Out with C++, Third Edition, Tony Gaddis Program 4-9 // This program uses the modulus operator to determine // if a number is odd or even. If the number is evenly // divided by 2, it is an even number. A remainder // indicates it is odd. #include void main(void) { int number; cout << "Enter an integer and I will tell you if it\n"; cout << "is odd or even. "; cin >> number; if (number % 2 == 0) cout << number << " is even.\n"; else cout << number << " is odd.\n"; }

Resource: Starting Out with C++, Third Edition, Tony Gaddis 4-9 Program Output with Example Input Enter an integer and I will tell you if it is odd or even. 17 [Enter] 17 is odd.

Resource: Starting Out with C++, Third Edition, Tony Gaddis 4.6The if/else if Construct The if/else if statement is a chain of if statements. The perform their tests, one after the other, until one of them is found to be true. If (expression) statement or block of statements; else if (expression) statement or block of statements; // put as many else it’s as needed here else if (expression) statement or block of statements;

Resource: Starting Out with C++, Third Edition, Tony Gaddis Program 4-11 // This program uses an if/else if statement // to assign a letter grade (A, B, C, D, or F) // to a numeric test score. #include void main(void) { int testScore; char grade; cout << "Enter your numeric test score and I will\n"; cout << "tell you the letter grade you earned: "; cin >> testScore;

Resource: Starting Out with C++, Third Edition, Tony Gaddis if (testScore < 60) grade = 'F'; else if (testScore < 70) grade = 'D'; else if (testScore < 80) grade = 'C'; else if (testScore < 90) grade = 'B'; else if (testScore <= 100) grade = 'A'; cout << "Your grade is " << grade << ".\n"; } 4-11 Program Continued

Resource: Starting Out with C++, Third Edition, Tony Gaddis 4-11 Program Output with Example Input Enter your test score and I will tell you the letter grade you earned: 88 [Enter] Your grade is B.

Resource: Starting Out with C++, Third Edition, Tony Gaddis Program 4-13 //This program uses an if/else if statement to //assign a letter grade ( A, B, C, D, or F ) //to a numeric test score. #include void main(void) { int testScore; cout << "Enter your test score and I will tell you\n"; cout << "the letter grade you earned: "; cin >> testScore; if (testScore < 60) { cout << "Your grade is F.\n"; cout << "This is a failing grade. Better see your "; cout << "instructor.\n"; } else if (testScore < 70) { cout << "Your grade is D.\n"; cout << "This is below average. You should get "; cout << "tutoring.\n"; }

Resource: Starting Out with C++, Third Edition, Tony Gaddis else if (testScore < 80) { cout << "Your grade is C.\n"; cout << "This is average.\n"; } else if(testScore < 90) { cout << "Your grade is B.\n"; cout << "This is an above average grade.\n"; } else if (testScore <= 100) { cout << "Your grade is A.\n"; cout << "This is a superior grade. Good work!\n"; } 4-13 Program Continued

Resource: Starting Out with C++, Third Edition, Tony Gaddis 4-13 Program Output with Example Input Enter your test score and I will tell you the letter grade you earned: 94 [Enter] Your grade is A. This is a superior grade. Good work!

Resource: Starting Out with C++, Third Edition, Tony Gaddis 4.7 Using a Trailing else A trailing else, placed at the end of an if/else if statement, provides default action when none of the if’s have true expressions.

Table 4-6: Lists C++’s Logical Operators

Resource: Starting Out with C++, Third Edition, Tony Gaddis cout << "Have you graduated from college "; cout << "in the past two years? "; cin >> recentGrad; if (employed == 'Y‘ && recentGrad == 'Y') // && Operator { cout << "You qualify for the special "; cout << "interest rate.\n"; } else { cout << "You must be employed and have \n"; cout << "graduated from college in the\n"; cout << "past two years to qualify.\n"; } 4-18 Program Continued

Table 4-9: Shows a Truth Table for the 1 Operator

Resource: Starting Out with C++, Third Edition, Tony Gaddis Program 4-20 //This program asks the user for his annual income and //the number of years he has been employed at his current job. //The ! operator reverses the logic of the expression in the if/else //statement. #include void main(void) { float income; int years; cout << "What is your annual income? "; cin >> income; cout << "How many years have you worked at " << "your current job? "; cin >> years; if (!(income >= || years > 5)) // Uses the ! Logical operator { cout << "You must earn at least $35,000 or have\n"; cout << "been employed for more than 5 years.\n"; } else cout << "You qualify.\n"; }

Table 4-10: Precedence of Logical Operators ! && ||