Chapter 4: Making Decisions.

Slides:



Advertisements
Similar presentations
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Chapter 4: Making Decisions.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 4: Making Decisions.
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.
07 Further testing1June Further selection CE : Fundamental Programming Techniques.
Selection Structures: Switch CSC 1401: Introduction to Programming with Java Week 4 – Lecture 1 Wanda M. Kunkle.
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.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
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.
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.
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.
Flow of Control Part 1: Selection
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.
Chapter 05 (Part III) Control Statements: Part II.
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 © 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.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
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.
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.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Java Programming Fifth Edition
Chapter 2 Variables.
Chapter 4: Control Structures I
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.
CMPT 201 if-else statement
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 4: Making Decisions.
Java Programming: Guided Learning with Early Objects
Control Structures.
Chapter 4: Making Decisions
Control Statement Examples
Chapter 4: Control Structures I
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 5 Decision Making and Branching
Chapter 2 Variables.
Chapter 7 Conditional Statements
Topics 4.1 Relational Operators 4.2 The if Statement
Chapter 3: Selection Structures: Making Decisions
CPS125 Week 5.
Chapter 3: Selection Structures: Making Decisions
Chapter 2 Variables.
The switch Statement When we want to compare a variable against several values to see which one it has, we can use the switch statement: switch (status)
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

Chapter 4: Making Decisions

4.10 Menus

Menus Menu-driven program: program execution controlled by user selecting from a list of actions Menu: list of choices on the screen Menus can be implemented using if/else if statements

Menu-Driven Program Organization Display list of numbered or lettered choices for actions Prompt user to make selection Test user selection in expression if a match, then execute code for action if not, then go on to next expression

4.11 Validating User Input

Validating User Input Input validation: inspecting input data to determine whether it is acceptable Bad output will be produced from bad input Can perform various tests: Range Reasonableness Valid menu choice Divide by zero

Input Validation in Program 4-19

Comparing Characters and Strings 4.12 Comparing Characters and Strings

Comparing Characters Characters are compared using their ASCII values 'A' < 'B' The ASCII value of 'A' (65) is less than the ASCII value of 'B'(66) '1' < '2' The ASCII value of '1' (49) is less than the ASCI value of '2' (50) Lowercase letters have higher ASCII codes than uppercase letters, so 'a' > 'Z'

Relational Operators Compare Characters in Program 4-20

Comparing string Objects Like characters, strings are compared using their ASCII values string name1 = "Mary"; string name2 = "Mark"; The characters in each string must match in length before they are equated! name1 > name2 // true name1 <= name2 // false name1 != name2 // true

Relational Operators Compare Strings in Program 4-21

The Conditional (Ternary) Operator 4.13 The Conditional (Ternary) Operator

The Conditional Operator Can use to create short if/else statements Format: expr ? expr : expr; x<0 ? y=10 : z=20; First Expression: Expression to be tested 2nd Expression: Executes if first expression is true 3rd Expression: Executes if the first expression is false

The Conditional Operator The value of a conditional expression is The value of the second expression if the first expression is true The value of the third expression if the first expression is false Parentheses () may be needed in an expression due to precedence of conditional operator

The Conditional Operator in Program 4-22

4.14 The switch Statement

http://www.tutorialspoint.com/cplusplus/cpp_switch_statement.htm

The switch Statement Used to select among statements from several alternatives In some cases, can be used instead of if/else if statements Can work on: short, int, long char Can’t work on: float, double String RANGES

switch Statement Format switch (expression) //integer { case exp1: statement1; case exp2: statement2; ... case expn: statementn; default: statementn+1; }

The switch Statement in Program 4-23

http://www.tutorialspoint.com/cplusplus/cpp_switch_statement.htm

switch Statement Requirements 1) expression must be an integer variable or an expression that evaluates to an integer value exp1 through expn must be constant integer expressions or literals, and must be unique in the switch statement default is optional but recommended

switch Statement-How it Works 1) expression is evaluated The value of expression is compared against exp1 through expn. If expression matches value expi, the program branches to the statement following expi and continues to the end of the switch If no matching value is found, the program branches to the statement after default:

break Statement Used to exit a switch statement If it is left out, the program "falls through" the remaining statements in the switch statement

break and default statements in Program 4-25 Continued…

break and default statements in Program 4-25

Using switch in Menu Systems switch statement is a natural choice for menu-driven program: display the menu then, get the user's menu selection use user input as expression in switch statement use menu choices as expr in case statements