A First Book of C++ Chapter 4 Selection.

Slides:



Advertisements
Similar presentations
Chapter 4: Making Decisions.
Advertisements

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.
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)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Objectives You should be able to describe:
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
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
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.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
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.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Flow of Control Part 1: Selection
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 4: Control Structures I (Selection)
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.
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.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
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: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
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.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
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 Looping 5. The Increment and Decrement Operators 5.1.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
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.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
CNG 140 C Programming (Lecture set 3)
Chapter 3 Control Statements
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.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Making Decisions.
Control Structures: Selection Statement
Chapter 7 Conditional Statements
Chapter 4: Control Structures I (Selection)
Objectives You should be able to describe: The while Statement
Control Structures: Selection Statement
Presentation transcript:

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 The switch Statement Common Programming Errors A First Book of C++ 4th Edition

Relational Expressions All computers are able to compare numbers Can be used to create an intelligence-like facility Relational expressions: expressions used to compare operands Format: a relational operator connecting two variable and/or constant operands Examples of valid relational expressions: Age > 40 length <= 50 flag == done A First Book of C++ 4th Edition

Relational Expressions The following are examples of valid relational expressions: age>40 length<=50 width>7 3<4 flag==done idNum==682 day!=5 2.0>3.3 hours>40 The following examples are invalid: length=<50 // incorrect symbol 2.0>>3.3 // invalid relational operator flag= =done // spaces are not allowed A First Book of C++ 4th Edition

Relational Expressions Relational expressions are evaluated to yield a numerical result. A condition that’s interpreted as true evaluates to an integer value of 1, and a false condition evaluates to an integer value of 0. For example, because the relationship 3 < 4 is always true, this expression has a value of 1, and because the relationship 2.0 > 3.3 is always false, the value of the expression is 0. A First Book of C++ 4th Edition

Relational Expressions This can be verified by these statements: Cout << “The value of 3 < 4 is “ << (3 < 4); Cout << “\nThe value of 2.0 > 3.3 is “ << (2.0 > 3.3); These statements result in the following display: The value of 3 < 4 is 1 The value of 2.0 > 3.3 is 0 A First Book of C++ 4th Edition

Relational Expressions (cont'd.) A First Book of C++ 4th Edition

Relational Expressions (cont'd.) Relational expressions (conditions): Are evaluated to yield a numerical result Condition that is true evaluates to 1 Condition that is false evaluates to 0 Example: The relationship 2.0 > 3.3 is always false; therefore, the expression has a value of 0 A First Book of C++ 4th Edition

Logical Operators More complex conditions can be created using logical operators AND, OR, and NOT Represented by the symbols: &&, ||, ! AND operator, &&: Used with two simple expressions Example: (age > 40) && (term < 10) Compound condition is true (has value of 1) only if age > 40 and term < 10 A First Book of C++ 4th Edition

Logical Operators (cont'd.) OR operator, ||: Used with two simple expressions Example: (age > 40) || (term < 10) Compound condition is true if age > 40 or if term < 10 or if both conditions are true NOT operator, !: Changes an expression to its opposite state If expression is true, then !expression is false A First Book of C++ 4th Edition

Logical Operators (cont'd.) A First Book of C++ 4th Edition

Logical Operators (cont'd.) A First Book of C++ 4th Edition

A Numerical Accuracy Problem Avoid testing equality of single- and double-precision values and variables using == operator Tests fail because many decimals cannot be represented accurately in binary For real operands: The expression operand_1 == operand_2 should be replaced by: abs(operand_1 – operand_2) < EPSILON If this expression is true for very small EPSILON, then the two operands are considered equal A First Book of C++ 4th Edition

The if-else Statement Selects between two statements based on the results of a comparison General form: if (expression) statement1; else statement2; If the value of expression is true, statement1 is executed If the value is false, statement2 is executed A First Book of C++ 4th Edition

The if-else Statement A First Book of C++ 4th Edition

The if-else Statement As an example, if the New Jersey state income tax is assessed at 2% of taxable income for incomes less than or equal to $20,000. For taxable incomes greater than $20,000, state taxes are 2.5% of the income exceeding $20,000 plus a fixed amount of $400. A First Book of C++ 4th Edition

A First Book of C++ 4th Edition

The if-else Statement (cont'd.) Program 4.1 run twice with different input data Result 1: Please type in the taxable income: 10000 Taxes are $ 200.00 Result 2: Please type in the taxable income: 30000 Taxes are $ 650.00 A First Book of C++ 4th Edition

Compound Statements Sequence of single statements between braces A First Book of C++ 4th Edition

Compound Statements The following program checks whether the value in tempType is f. If so, the compound statement corresponding to the if part of the if-else statement is executed. Any other letter in tempType results in executing the compound statement corresponding to the else part. A First Book of C++ 4th Edition

A First Book of C++ 4th Edition

Compound Statements (cont'd.) Output of Program 4.2 Enter the temperature to be converted: 212 Enter an f if the temperature is in Fahrenheit or a c if the temperature is in Celsius: f The equivalent Celsius temperature is 100.00 A First Book of C++ 4th Edition

Block Scope Block of code: all statements contained within a compound statement Any variable declared within a block has meaning only between its declaration and the closing braces of the block Example with two blocks of code A First Book of C++ 4th Edition

Block Scope (cont'd.) { // start of outer block int a = 25; int b = 17; cout << “The value of a is ” << a << “ and b is ” << b << endl; { // start of inner block double a = 46.25; int c = 10; cout << “a is now ” << a << “ b is now ” << b << “ and c is ” << c << endl; } // end of inner block cout << “a is now ” << a << “ and b is ” << b << endl; } // end of outer block A First Book of C++ 4th Edition

Block Scope (cont'd.) Output of block scope example: The value of a is 25 and b is 17 a is now 46.25 b is now 17 and c is 10 a is now 25 and b is 17 A First Book of C++ 4th Edition

One-Way Selection A modification of if-else that omits else part if statement takes the form: if (expression) statememt; Modified form called a one-way statement The statement following if (expression) is executed only if the expression is true The statement may be a compound statement A First Book of C++ 4th Edition

One-Way Selection A First Book of C++ 4th Edition

One-Way Selection (cont'd.) A First Book of C++ 4th Edition

One-Way Selection (cont'd.) Program 4.3 run twice with different input data Result 1: Please type in car number and mileage: 256 3562.8 Car 256 is over the limit. End of program output. Result 2: Please type in car number and mileage: 23 2562.8 A First Book of C++ 4th Edition

Problems Associated with the if-else Statement Most common problems: Misunderstanding what an expression is Using the assignment operator, =, in place of the relational operator, == Example: Initialize age = 18 The expression (age = 40) sets age to 40 Does not compare age to 40 Has a value of 40 (true) Produces invalid results if used in if-else statement A First Book of C++ 4th Edition

Problems Associated with the if-else Statement (cont'd.) Example (cont'd.): The expression (age == 40) compares age to 40 Has a value of 0 (false) This expression will produce a valid test in an if-else statement A First Book of C++ 4th Edition

Nested if Statements if-else statement can contain simple or compound statements Another if-else statement can be included Example: if (hours < 9) { if (distance > 500) cout << “snap”; } else cout << “pop”; A First Book of C++ 4th Edition

The if-else Chain Format: if (expression_1) statement1; else if (expression_2) statement2; else statement3; Chain can be extended indefinitely by making last statement another if-else statement A First Book of C++ 4th Edition

The if-else Chain To illustrate using an if-else chain, Program 4.4 displays a person’s marital status corresponding with a letter input. The following letter codes are used: A First Book of C++ 4th Edition

The if-else Chain As another example of an if-else chain, take a look at determining the monthly income of a salesperson by using the following commission schedule: A First Book of C++ 4th Edition

A First Book of C++ 4th Edition

The if-else Chain As another example of an if-else chain, take a look at determining the monthly income of a salesperson by using the following commission schedule: A First Book of C++ 4th Edition

The switch Statement Format: switch (expression) { // start of compound statement case value_1: <- terminated with a colon statement1; statement2; break; case value_2: <- terminated with a colon statementm; default: <- terminated with a colon statementaa; } // end of switch and compound statement A First Book of C++ 4th Edition

The switch Statement (cont'd.) Four new keywords used: switch, case, default, and break Function: Expression following switch is evaluated Must evaluate to an integer result Result compared sequentially to alternative case values until a match found Statements following matched case are executed When break statement reached, switch terminates If no match found, default statement block is executed A First Book of C++ 4th Edition

A First Book of C++ 4th Edition

The switch Statement (cont'd.) Program 4.6 results: Please type in two numbers: 12 3 Enter a select code: 1 for addition 2 for multiplication 3 for division : 2 The product of the numbers entered is 36 A First Book of C++ 4th Edition

Common Programming Errors Using the assignment operator, =, in place of the relational operator, == Assuming that the if-else statement is selecting an incorrect choice when the problem is really the values being tested Using nested if statements without including braces to clearly indicate the desired structure A First Book of C++ 4th Edition

Summary Relational expressions (conditions): Are used to compare operands A condition that is true has a value of 1 A condition that is false has a value of 0 More complex conditions can be constructed from relational expressions using C++’s logical operators, && (AND), || (OR), and ! (NOT) if-else statements select between two alternative statements based on the value of an expression A First Book of C++ 4th Edition

Summary (cont'd.) if-else statements can contain other if-else statements If braces are not used, each else statement is associated with the closest unpaired if if-else chain: a multi-way selection statement Each else statement (except for the final else) is another if-else statement Compound statement: any number of individual statements enclosed within braces A First Book of C++ 4th Edition

Summary (cont'd.) Variables have meaning only within the block where they are declared Includes any inner blocks switch statement: multiway selection statement The value of an integer expression is compared to a sequence of integer or character constants or constant expressions Program execution transferred to first matching case Execution continues until optional break statement is encountered A First Book of C++ 4th Edition

Chapter Supplement: A Closer Look at Testing A comprehensive set of test runs would reveal all possible program errors Ensuring that a program works correctly for any combination of input and computed data This goal is usually impossible Except for extremely simple programs At a minimum, test data should include: Suitable values for input data Illegal input values that the program should reject Limiting values that are checked in the program A First Book of C++ 4th Edition