Title: Chapter 5 Decision Making
1Chapter 5 Decision Making
2Simple if Statement
- Capable of making decisions
- Relational expression evaluated
- Evaluated as true or false
if (relational expression)
statement(s)
Lesson 5.1
3Relational Expressions
- Type of logical expression
- Produces result of true or false
- Compares values of two arithmetic expressions
left_operand relational_operator right_operand
Lesson 5.1
4Relational Operators
- lt less than
- lt less than or equal to
- equal to
- gt greater than
- gt greater than or equal to
- ! not equal
Lesson 5.1
5Simple If/Else Statement
if (expression) statement1a
statement1b else
statement1a statement1b
- Provides block of statements to be executed for
either true OR false - Braces optional if only one statement in block
Lesson 5.2
6if Examples
- x 3
- if (x lt 2)
-
- cout ltlt x is smaller than 2
-
Simple if statements only done on true,
but when false, skips to end of if
Full if code for when true and also when false
- if (x lt 12)
- cout ltlt smaller than twelve
- else
- cout ltlt twelve or larger
7Ternary Operator
- Three operands needed for proper usage
- Conditional ? operator
- Question mark separates relational expression
from rest of statement - Colon separates two operands
expression1? expression2 expression3
x (y lt z) ? y z
Lesson 5.2
8Example
if (a gt b) ans 10 else ans
25
a gt b ? (ans 10) (ans 25)
ans (a gt b) ? 10 25
Lesson 5.2
9Nested if-else Statements
if (outer) if (inner)
else
else
Lesson 5.3
10Three Logical Operators
- Exclamation mark !
- NOT (negation)
- unary
- Two ampersands
- AND (conjunction)
- binary
- Two vertical pipes
- OR (inclusive disjunction)
- binary
Lesson 5.4
11Logical NOT Operator
- Reverses result of relational expression
- Example ! (x y)
Evaluate relational expression, does 3 equal 7?
! ( false ) so negates to true
! ( true ) so negates to false
Lesson 5.4
12Logical AND Operator
- Combines two relational expressions
- Combined expression true only if BOTH expressions
are true
true
false
false
expression1 expression2
false
false true
false
false false
true true
true
Lesson 5.4
13Logical OR Operator
- Combines two relational expressions
- Combined expression false only if BOTH
expressions are false
true
false
true
expression1 expression2
true
false true
false false
false
true true
true
Lesson 5.4
14Values of Relational Expressions
- Result of relational expression
- False, C compiler gives zero
- True, C compiler gives one
- Value of relational expression
- Zero, result is false
- Nonzero, result is true
- any number other than zero, including negatives
Lesson 5.5
15Precedence of Operators
( ) Parentheses L to R 1
, -- Postincrement L to R 2 ,
-- Preincrement R to L 3
! Logical NOT L to R 3 ,
- Positive, negative L to R 3 , /,
Multiplication, division L to R 4
, - Addition, subtraction L to R 5 lt,
gt, gt, lt Relational operator L to R 6
, ! Relational operator L to R 7
Logical AND L to R 8
Logical OR L to R 9 , -, , /,
Compound assignment R to L 10
Assignment R to L 10
Lesson 5.5
16Example
Assume a 4, b -2, and c 0 x (a gt b
b gt c a b)
x ((a gt b) (b gt c) (a b))
Group
x ((4 gt -2) (-2 gt 0) (4 -2))
x (TRUE (FALSE FALSE))
Group
x (TRUE FALSE)
x (TRUE)
x 1
Lesson 5.5
17Example of Single Variable
- logical value
- False if value is 0
- True if value is nonzero
if (c) statement1 else statement2
Statement2 executed if c has value of zero,
statement1 is executed if c has any other value
but zero.
Lesson 5.5
18if-else-if
- Shifts program control, step by step, through
series of statement blocks - Control stops at relational expression that tests
true
Lesson 5.6
19if-else-if Form
if (relational_expression_1)
statement_block_1 else if
(relational_expression_2)
statement_block_2 . .
. else if (relational_expression_n-1)
statement_block_n-1 else
statement_block_n
false
else
false
else
true
Lesson 5.6
20switch Control Structure
- Constructed like if-else-if
- Used to transfer control
- Can nest switch control structures
- Keyword switch followed by expression
switch (expression)
statement block
Lesson 5.6
21switch Statement Block Syntax
switch (expression) case constant1
statement1a
statement1b
case constant2 statement2a
statement2b
default
statements
- Keyword case only used in switch statement
- case used to form label
- case label is constant followed by colon
- Constant1, constant2, etc must be integer
expressions - Constant expressions must be unique
- default optional, used when no match is found
Lesson 5.6
22break Statement
- Terminates execution of switch statement
- Sends control to point of closing brace
- Usually last statement for each case
- If no break, then statements in next case executed
switch (expression) case constant1
statement1a
break case
constant2 statement2a
break
default statements
23bool Data Type
- Named after mathematician George Boole
- Can contain one of only two values (0 or 1)
- true or false, fail or pass, off or on
- Declare by using keyword bool
- Example bool salty, hard, acidic
- Assign value
- Example acidic (pH lt 7)
Lesson 5.7
24bool Variables
- Assign keywords true or false
- good_taste true
- Assign bools to int variables
- int i
- i good_taste
- Print bool variables values using cout
- Prints 1 for true or 0 for false
- boolalpha manipulator gives words true or false
Lesson 5.7
25Summary
Learned how to
- Create simple if and if-else statements
- Create relational expressions
- Utilize logical operators
- Interpret the order of precedence for all
operators (arithmetic, relational, and logical) - Create multiple decision statements (if-else-if
or switch statements) - Utilize bool data type