C++ for Engineers and Scientists Third Edition

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
true (any other value but zero) false (zero) expression Statement 2
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
Visual C++ Programming: Concepts and Projects
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Decision Structures and Boolean Logic
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
JavaScript, Fourth Edition
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
A FIRST BOOK OF C++ CHAPTER 5 REPETITION. OBJECTIVES In this chapter, you will learn about: The while Statement Interactive while Loops The for Statement.
A First Book of C++ Chapter 5 Repetition.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
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.
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)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
THE DECISIONS CONTROL STRUCTURE! CHAPTER 2. Transfer of control statement: o The statement of computer program are executed one after the other in the.
Java Programming Fifth Edition
CNG 140 C Programming (Lecture set 3)
More on the Selection Structure
Selections Java.
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.
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Control Structures I (Selection)
The Selection Structure
Chapter 5: The Selection Structure
Presentation transcript:

C++ for Engineers and Scientists Third Edition Chapter 4 Selection Structures 1 1

Objectives In this chapter, you will learn about: Selection criteria The if-else statement Nested if statements The switch statement Program testing Common programming errors C++ for Engineers and Scientists, Third Edition

Selection Criteria if-else statement: Implements a decision structure for two alternatives Syntax: if (condition) statement executed if condition is true; else statement executed if condition is false; C++ for Engineers and Scientists, Third Edition

Table 4.1 C++’s Relational Operators Relational expression: Compares two operands or expressions using relational operators Table 4.1 C++’s Relational Operators C++ for Engineers and Scientists, Third Edition

Relational Operators (continued) Relational expressions are evaluated to a numerical value of 1 or 0 only: If the value is 1, the expression is true If the value is 0, the expression is false char values are automatically coerced to int values for comparison purposes Strings are compared on a character by character basis The string with the first lower character is considered smaller C++ for Engineers and Scientists, Third Edition

Logical Operators AND (&&): Condition is true only if both expressions are true OR (||): Condition is true if either one or both of the expressions is true NOT (!): Changes an expression to its opposite state; true becomes false, false becomes true C++ for Engineers and Scientists, Third Edition

Logical Operators (continued) Table 4.2 Operator Precedence and Associativity C++ for Engineers and Scientists, Third Edition

The if-else Statement if-else performs instructions based on the result of a comparison Place statements on separate lines for readability Syntax: C++ for Engineers and Scientists, Third Edition

The if-else Statement (continued) C++ for Engineers and Scientists, Third Edition

Compound Statements Compound statement: A sequence of single statements contained between braces Creates a block of statements Block of statements can be used anywhere that a single statement is legal Any variable declared within a block is usable only within that block Scope: The area within a program where a variable can be used A variable’s scope is based on where the variable is declared C++ for Engineers and Scientists, Third Edition

Problems Associated with the if-else Statement Common problems with if-else statements: Misunderstanding what an expression is Using the assignment operator (=) instead of the relational operator (==) C++ for Engineers and Scientists, Third Edition

Nested if Statements if-else statement can contain any valid C++ statement, including another if-else Nested if statement: an if-else statement completely contained within another if-else Use braces to block code, especially when inner if statement does not have its own else C++ for Engineers and Scientists, Third Edition

The if-else Chain if-else chain: A nested if statement occurring in the else clause of the outer if-else If any condition is true, the corresponding statement is executed and the chain terminates Final else is only executed if no conditions were true Serves as a catch-all case if-else chain provides one selection from many possible alternatives C++ for Engineers and Scientists, Third Edition

The if-else Chain (continued) General form of an if-else chain C++ for Engineers and Scientists, Third Edition

Single Alternative Double Alternative: Multiple Alternative if <condition> { <statements ;> } if <condition> { <statements ;> } else if <condition > <statements > if<condition> { <statements ;> } else

The switch Statement switch statement: Provides for one selection from many alternatives switch keyword starts the statement Is followed by the expression to be evaluated case keyword identifies a value to be compared to the switch expression When a match is found, statements in this case block are executed All further cases after a match is found are executed unless a break statement is found C++ for Engineers and Scientists, Third Edition

The switch Statement (continued) default case is executed if no other case value matches were found default case is optional C++ for Engineers and Scientists, Third Edition

A Case Study: Solving Quadratic Equations Data validation: Use defensive programming techniques to validate user input Includes code to check for improper data before an attempt is made to process it further Solving quadratic equations: Use the software development procedure to solve for the roots of a quadratic equation C++ for Engineers and Scientists, Third Edition

A Closer Look: Program Testing Theory: A comprehensive set of test runs would test all combinations of input and computations, and would reveal all errors Reality: There are too many combinations to test for any program except a very simple one Example: One program with 10 modules, each with five if statements, always called in the same order There are 25 paths through each module, and more than 250 paths through the program! C++ for Engineers and Scientists, Third Edition

A Closer Look: Program Testing (continued) Conclusion: there is no error-free program, only one in which no errors have recently been encountered C++ for Engineers and Scientists, Third Edition

Common Programming Errors Using the assignment operator (=) instead of the relational operator (==) for an equality test Placing a semicolon immediately after the condition Assuming a structural problem with an if-else causes the error instead of focusing on the data value being tested Using nested if statements without braces to define the structure C++ for Engineers and Scientists, Third Edition

Summary Relational expressions, or conditions, are used to compare operands If the relation expression is true, its value is 1; if false, its value is 0 Use logical operators && (AND), || (OR), and ! (NOT) to construct complex conditions if-else allows selection between two alternatives C++ for Engineers and Scientists, Third Edition

Summary (continued) An if expression that evaluates to 0 is false; if non-zero, it is true if statements can be nested Chained if statement provides a multiway selection Compound statement: contains any number of individual statements enclosed in braces C++ for Engineers and Scientists, Third Edition

Summary (continued) switch statement: Provides a multiway selection switch expression: Evaluated and compared to each case value If a match is found, execution begins at that case’s statements and continues unless a break is encountered C++ for Engineers and Scientists, Third Edition