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

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 4: Making Decisions.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
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.
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.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Fundamental Programming Fundamental Programming More on Selection.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions Continued.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
1 CSC103: Introduction to Computer and Programming Lecture No 11.
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.
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.
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.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
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.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Chapter Making Decisions 4. Relational Operators 4.1.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Input Validation 10/09/13. Input Validation with if Statements You, the C++ programmer, doing Quality Assurance (by hand!) C++ for Everyone by Cay Horstmann.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions Starting Out with C++ Early Objects Seventh Edition.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
Control Flow Statements
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.
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.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Java Programming Fifth Edition
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
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Chapter 4: Making Decisions
Alternate Version of STARTING OUT WITH C++ 4th Edition
Selection Control Structure: Switch Case Statement
Topics 4.1 Relational Operators 4.2 The if Statement
Alternate Version of STARTING OUT WITH C++ 4th Edition
Selection Control Structure
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
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.9 Validating User Input 4.10 More About Variable Definitions and Scope 4.11 Comparing Characters and Strings 4.13 The switch Statement

Chapter 4 slide Validating User Input Input validation: inspecting input data to determine if it is acceptable Want to avoid accepting bad input Can perform various tests –Range –Reasonableness –Valid menu choice –Divide by zero

Chapter 4 slide More About Variable Definitions and Scope Scope of a variable is the block in which it is defined, from the point of definition to the end of the block Usually defined at beginning of function May be defined close to first use

Chapter 4 slide 5 More About Variable Definitions and Scope Variables defined inside { } have local or block scope When in a block nested inside another block, you can define variables with the same name as in the outer block. –When in the inner block, the outer definition is not available –Not a good idea

Chapter 4 slide Comparing Characters and Strings Can use relational operators with characters and string objects if (firstName < “Beth”) Comparing characters is really comparing ASCII values of characters Comparing string objects is comparing the ASCII values of the characters in the strings. Comparison is character-by-character

Chapter 4 slide The switch Statement Used to select among statements from several alternatives May sometimes be used instead of if/else if statements

Chapter 4 slide 8 switch Statement Format switch (expression) { case exp1: statement set 1; case exp2: statement set 2;... case expn: statement set n; default: statement set n+1; }

Chapter 4 slide 9 switch Statement Requirements 1) expression must be a char or an integer variable or an expression that evaluates to an integer value 2) exp1 through expn must be constant integer expressions and must be unique in the switch statement 3) default is optional, but recommended

Chapter 4 slide 10 How the switch Statement Works 1)expression is evaluated 2)The value of expression is compared against exp1 through expn. 3)If expression matches value expi, the program branches to the statement(s) following expi and continues to the end of the switch 4)If no matching value is found, the program branches to the statement after default:

Chapter 4 slide 11 The break Statement Used to stop execution in the current block Also used to exit a switch statement Useful to execute a single case statement without executing statements following it

Chapter 4 slide 12 Example switch Statement switch (gender) { case ‘f’: cout << “female”; break; case ‘m’: cout << “male”; break; default : cout << “invalid gender”; }

Chapter 4 slide 13 Using switch with a Menu switch statement is a natural choice for menu-driven program –display menu –get user input –use user input as expression in switch statement –use menu choices as exp to test against in the case statements

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