Lecture 4 - Selection Structures if and switch Statements PDF

Summary

This document is a lecture about selection structures, if and switch statements, related to programming concepts of C++. The lecture aims to explain and demonstrate the usage of these statements from a programming perspective.

Full Transcript

Lecture 4: Selection Structures: if and switch Statements Problem Solving, Abstraction, and Design using C++ 6e by Frank L. Friedman and Elliot B. Koffman Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Obje...

Lecture 4: Selection Structures: if and switch Statements Problem Solving, Abstraction, and Design using C++ 6e by Frank L. Friedman and Elliot B. Koffman Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Objectives Become familiar with selection statements Compare numbers, characters, and strings Use relational, equality, and logical operators Write selection statements with one or two alternatives Select among multiple choices Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2 4.1 Control Structures Regulate the flow of execution Combine individual instructions into a single logical unit with one entry point and one exit point. Three types: sequential, selection, repetition Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3 Sequential Execution Each statement is executed in sequence. A compound statement is used to specify sequential control: { statement1; statement2;... statementn; } Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 4.2 Logical Expressions C++ control structure for selection is the if statement. E.g.: if (weight > 100.00) shipCost = 10.00; else shipCost = 5.00; Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5 Relational and Equality Operators Logical expressions (conditions) are used to perform tests for selecting among alternative statements to execute. Typical forms: variable relational-operator variable variable relational-operator constant variable equality-operator variable variable equality-operator constant Evaluate to Boolean (bool) value of true or false Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6 Table 4.1 Rational and Equality Operators Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7 Example x -5 -5 x = y false Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9 Logical Operators && (and) || (or) ! (not) Used to form more complex conditions, e.g. (salary < minSalary) || (dependents > 5) (temperature > 90.0) && (humidity > 0.90) winningRecord && (!probation) Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10 Table 4.3 && Operator Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11 Table 4.4 || Operator Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12 Table 4.5 ! Operator Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13 Table 4.6 Operator Precedence Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14 Example x y z flag 3.0 4.0 2.0 false x + y / z = x - z) true 6.0 1.0 true true Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 16 Table 4.7 English Conditions as C++ Expressions Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 17 Comparing Characters and Strings Letters are in typical alphabetical order Upper and lower case significant Digit characters are also ordered as expected String objects require string library – Compares corresponding pairs of characters Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18 Table 4.8 Examples of Comparisons Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 19 Boolean Assignment Assignment statements have general form variable = expression; E.g.: (for variable called same of type bool) same = true; same = (x == y); Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 20 Additional Examples inRange = (n > -10) && (n < 10); isLetter = ((‘A’

Use Quizgecko on...
Browser
Browser