Decision

Decision - C+ Programming: Control Structures I (Selection)...

Info iconThis preview shows pages 1–21. Sign up to view the full content.

View Full Document Right Arrow Icon
C++ Programming: Control Structures I (Selection)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
C++ Programming: From Problem Analysis to Program Design, Third Edition 2 Objectives In this chapter you will: Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean) expressions Discover how to use the selection control structures if , if ... else , and switch in a program Learn to use the assert function to terminate a program
Background image of page 2
C++ Programming: From Problem Analysis to Program Design, Third Edition 3 Control Structures A computer can proceed: In sequence Selectively (branch) - making a choice Repetitively (iteratively) - looping Some statements are executed only if certain conditions are met A condition is represented by a logical (Boolean) expression that can be true or false A condition is met if it evaluates to true
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
C++ Programming: From Problem Analysis to Program Design, Third Edition 5 Relational Operators Relational operators: Allow comparisons Require two operands (binary) Return 1 if expression is true , 0 otherwise Comparing values of different data types may produce unpredictable results For example, 8 < '5' should not be done Any nonzero value is treated as true
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
C++ Programming: From Problem Analysis to Program Design, Third Edition 8 Comparing string Types Relational operators can be applied to strings Strings are compared character by character, starting with the first character Comparison continues until either a mismatch is found or all characters are found equal If two strings of different lengths are compared and the comparison is equal to the last character of the shorter string The shorter string is less than the larger string
Background image of page 8
9 string Comparison Example Suppose we have the following declarations: string str1 = "Hello"; string str2 = "Hi"; string str3 = "Air"; string str4 = "Bill";
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 10
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 12
13 Logical (Boolean) Operators Logical (Boolean) operators enable you to combine logical expressions Three logical (Boolean) operators: ! - not – and || - or Logical operators take logical values as operands and yield logical values as results ! is unary; and || are binary operators Putting ! in front of a logical expression reverses its value
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 14
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 16
Background image of page 17

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
18 Precedence of Operators Relational and logical operators are evaluated from left to right The associativity is left to right Parentheses can override precedence
Background image of page 18
Background image of page 19

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
20 Short-Circuit Evaluation Short-circuit evaluation : evaluation of a logical expression stops as soon as the value of the expression is known Example: (age >= 21) || ( x == 5) //Line 1 //Line 2
Background image of page 20
Image of page 21
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/04/2011 for the course CSCI 101 taught by Professor Ghyam during the Spring '08 term at USC.

Page1 / 66

Decision - C+ Programming: Control Structures I (Selection)...

This preview shows document pages 1 - 21. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online