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

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

1 Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems/Headington

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

View Full Document
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
3 Flow of Control the order in which program statements are executed WHAT ARE THE POSSIBILITIES. . .

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

View Full Document
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)
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

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

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

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

View Full Document
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
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)

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

View Full Document
10 In C++ the value 0 represents false ANY non-zero value represents true
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

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

View Full Document
12 string myState; string yourState; myState = “Texas”; yourState = “Maryland”; EXPRESSION VALUE myState == yourState false myState > yourState true myState == “Texas” true myState < “texas” true
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

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

View Full Document
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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online