9781423902096_PPT_ch04 - C+ Programming: From Problem...

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

View Full Document Right Arrow Icon
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: 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, Fourth 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, Fourth 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 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
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 4 Control Structures (continued)
Background image of page 4
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 5 Relational Operators A condition is represented by a logical (Boolean) expression that can be true or false Relational operators: Allow comparisons Require two operands (binary) Evaluate to true or false
Background image of page 5

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, Fourth Edition 6 Relational Operators (continued)
Background image of page 6
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 7 Relational Operators and Simple Data Types You can use the relational operators with all three simple data types: 8 < 15 evaluates to true 6 != 6 evaluates to false 2.5 > 5.8 evaluates to false 5.9 <= 7.5 evaluates to true
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, Fourth Edition 8 Comparing Floating-Point Numbers for Equality Comparison of floating-point numbers for equality may not behave as you would expect Example: 1.0 == 3.0/7.0 + 2.0/7.0 + 2.0/7.0 evaluates to false Why? 3.0/7.0 + 2.0/7.0 + 2.0/7.0 = 0.99999999999999989 Solution: use a tolerance value Example: fabs(x – y) < 0.000001
Background image of page 8
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 9 Comparing Characters
Background image of page 9

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, Fourth Edition 10 Relational Operators and the string Type 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 10
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 11 Relational Operators and the string Type (continued) Suppose we have the following declarations: string str1 = "Hello"; string str2 = "Hi"; string str3 = "Air"; string str4 = "Bill"; string str4 = "Big";
Background image of page 11

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, Fourth Edition 12 Relational Operators and the string Type (continued)
Background image of page 12
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 13 Relational Operators and the string Type (continued)
Background image of page 13

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, Fourth Edition 14 Relational Operators and the string Type (continued)
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/23/2011 for the course CHEMICAL E 2 taught by Professor Hibasaadeh during the Spring '11 term at University of Jordan.

Page1 / 72

9781423902096_PPT_ch04 - C+ Programming: From Problem...

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

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