Selection Statements Conditional Expression... Selection Statements Conditional ExpressionSelection Criteria Selection criteria = value of expression ⇒ select appropriate flow of control
Trang 1Selection Statements
Conditional Expression
C++ Selection Structures
Department of Computer Science
HCMC University of Technology, Viet Nam
02, 2014
Trang 2Selection Statements Conditional Expression
Trang 3Selection Statements Conditional Expression
yes no
Trang 4Selection Statements Conditional Expression
Selection Criteria
Selection criteria = value of expression ⇒ select appropriate flow of control
C++
if statement: 2 values (true/false, 0/not 0)
switch statement: multiple discrete values
Trang 5Selection Statements Conditional Expression
Relational Operators
used to compare 2 operands
return Boolean value (true/false) (i.e bool type)
Trang 6Selection Statements Conditional Expression
Logical Operators
used to create more complex conditions
its operands in bool type
also return bool value
short-circuit evaluation
Trang 7Selection Statements Conditional Expression
Operator Precedence and Associativity
List in the decreasing order of the precedence
Trang 8Selection Statements Conditional Expression
Trang 9Selection Statements Conditional Expression
Order of evaluation for Arithmetic/Relational Expression
Higher precedence operators are evaluated before lower precedence operator
Trang 10Selection Statements Conditional Expression
Order of evaluation for Logical Expression
Short-circuit evaluation
&&: if the left operand is false ⇒ stop and return false
||: if the left operand is true ⇒ stop and return true
Trang 11Selection Statements Conditional Expression
The bool data type
C++ uses bool for Boolean type
The actual value of true is 1 and false is o
Trang 12The if-else statement
directs the computer to
select a statement based
no
Trang 13taxable income greater
than $20,000, taxes are
2.5% of the income that
exceeds $20,000 plus a
fixed amount of $400
StartInput taxable
taxable > CUTOFF
taxes = HIGHRATE * (taxable - CUTOFF) + FIXEDAMT
taxes= LOWRATE
* taxable
Output taxesEnd
yes
no
Trang 15Please t y p e i n t h e t a x a b l e income : 30000Taxes are $ 650
Trang 16no
Trang 22Selection Statements
Conditional Expression
If Statement
Switch Statement
Solve dangling else
Use block statement
if (exp1) {if (exp2) stmt1} else stmt2
if (exp1) {if (exp2) stmt1 else stmt2}
Use C++ convention: else matches with closest unmatch if
statement
Trang 23is based on the value of an expression
requires the expression in int type
Syntax:
switch ( < e x p r e s s i o n >) {
case < v a l u e 1 >: [ < statements > ]
[ break ; ] case < v a l u e 2 >: [ < statements > ]
[ break ; ] case < v a l u e 3 >: [ < statements > ]
Trang 24is based on the value of an expression
requires the expression in int type
Syntax:
switch ( < e x p r e s s i o n >) {
case < v a l u e 1 >: [ < statements > ]
[ break ; ] case < v a l u e 2 >: [ < statements > ]
[ break ; ] case < v a l u e 3 >: [ < statements > ]
[ d e f a u l t : < s ta te men ts > ]
Then, compare to <value 1>
Trang 25is based on the value of an expression
requires the expression in int type
Syntax:
switch ( < e x p r e s s i o n >) {
case < v a l u e 1 >: [ < statements > ]
[ break ; ] case < v a l u e 2 >: [ < statements > ]
[ break ; ] case < v a l u e 3 >: [ < statements > ]
[ d e f a u l t : < s ta te men ts > ]
}
If match,execute <statements>
Trang 26is based on the value of an expression
requires the expression in int type
Syntax:
switch ( < e x p r e s s i o n >) {
case < v a l u e 1 >: [ < statements > ]
[ break ; ] case < v a l u e 2 >: [ < statements > ]
[ break ; ] case < v a l u e 3 >: [ < statements > ]
[ d e f a u l t : < s ta te men ts > ]
If not match, compare to <value 2>
Trang 27is based on the value of an expression
requires the expression in int type
Syntax:
switch ( < e x p r e s s i o n >) {
case < v a l u e 1 >: [ < statements > ]
[ break ; ] case < v a l u e 2 >: [ < statements > ]
[ break ; ] case < v a l u e 3 >: [ < statements > ]
Trang 28is based on the value of an expression
requires the expression in int type
Syntax:
switch ( < e x p r e s s i o n >) {
case < v a l u e 1 >: [ < statements > ]
[ break ; ] case < v a l u e 2 >: [ < statements > ]
[ break ; ] case < v a l u e 3 >: [ < statements > ]
[ d e f a u l t : < s ta te men ts > ]
If there is break,
go out of switch
Trang 29is based on the value of an expression
requires the expression in int type
Syntax:
switch ( < e x p r e s s i o n >) {
case < v a l u e 1 >: [ < statements > ]
[ break ; ] case < v a l u e 2 >: [ < statements > ]
[ break ; ] case < v a l u e 3 >: [ < statements > ]
Trang 34Selection Statements
Conditional Expression
Conditional Expression
There are 3 argument expressions
If the value of the first expression is true, evaluate and return the value of the second expression.
Otherwise, evaluate and return the value of the third.
Trang 35Selection Statements
Conditional Expression
Summary
Relational and logical expression
If-Else and If-No-Else statement
Nested if statement
Switch statement
Conditional Expression