CHAP05 - Chapter 5 Conditions, Logical Expressions, and...

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

View Full Document Right Arrow Icon
1 Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems/Headington
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Chapter 5 Topics Data Type bool Using Relational and Logical Operators to Construct and Evaluate Logical Expressions If-Then-Else Statements If-Then Statements Nested If Statements for Multi-way Branching Testing the State of an I/O Stream Testing a C++ Program
Background image of page 2
3 Flow of Control the order in which program statements are executed WHAT ARE THE POSSIBILITIES. . .
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Flow of Control is Sequential unless a “control structure” is used to change that there are 2 general types of control structures: Selection (also called branching) Repetition (also called looping)
Background image of page 4
5 bool Data Type type bool is a built-in type consisting of just 2 values, the constants true and false we can declare variables of type bool bool hasFever; // true if has high temperature bool isSenior; // true if age is at least 55
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 C++ control structures Selection if if . . . else switch Repetition for loop while loop do . . . while loop
Background image of page 6
7 Control Structures use logical expressions which may include: 6 Relational Operators < <= > >= == != 3 Logical Operators ! ||
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 are used in expressions of form: ExpressionA Operator ExpressionB temperature > humidity B * B - 4.0 * A * C > 0.0 abs (number ) ==      35 initial ! =      ‘Q’ 6 Relational Operators
Background image of page 8
9 int x, y ; x = 4; y = 6; EXPRESSION VALUE x < y true x + 2 < y false x != y true x + 3 >= y true y == x false y == x+2 true y = x + 3 7 (true)
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 In C++ the value 0 represents false ANY non-zero value represents true
Background image of page 10
11 Comparing Strings two objects of type string (or a string object and a C string) can be compared using the relational operators a character-by-character comparison is made using the ASCII character set values if all the characters are equal, then the 2 strings are equal. Otherwise, the string with the character with smaller ASCII value is the “lesser” string
Background image of page 11

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

View Full DocumentRight Arrow Icon
12 string myState; string yourState; myState = “Texas”; yourState = “Maryland”; EXPRESSION VALUE myState == yourState false myState > yourState true myState == “Texas” true myState < “texas” true
Background image of page 12
13 Operator Meaning Associativity ! NOT Right *, / , % Multiplication, Division, Modulus Left + , - Addition, Subtraction Left < Less than Left <= Less than or equal to Left > Greater than Left >= Greater than or equal to Left == Is equal to Left != Is not equal to Left AND Left || OR Left = Assignment Right
Background image of page 13

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

View Full DocumentRight Arrow Icon
LOGICAL EXPRESSION MEANING DESCRIPTION ! p NOT p ! p is false if p is true ! p is true if p is false p AND q both p and q are true. It is false otherwise.
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 06/13/2011 for the course CSC 140 taught by Professor Lebre during the Spring '04 term at Moraine Valley Community College.

Page1 / 81

CHAP05 - Chapter 5 Conditions, Logical Expressions, and...

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