Tải bản đầy đủ (.pdf) (35 trang)

Kỹ thuật lập trình Chương 4

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (111.48 KB, 35 trang )

Selection Criteria
Selection Statements
Conditional Expression

C++ Selection Structures
Department of Computer Science
HCMC University of Technology, Viet Nam

02, 2014

Department of Computer Science

C++ Selection Structures


Selection Criteria
Selection Statements
Conditional Expression

Outline

1

Selection Criteria

2

Selection Statements
If Statement
Switch Statement


3

Conditional Expression

Department of Computer Science

C++ Selection Structures


Selection Criteria
Selection Statements
Conditional Expression

Overview

action 1
action 1
no

action 2

condition
yes

action 3
action 3

Department of Computer Science

action 2


C++ Selection Structures


Selection Criteria
Selection 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

Department of Computer Science

C++ Selection Structures


Selection Criteria
Selection Statements
Conditional Expression

Relational Operators

used to compare 2 operands
return Boolean value (true/false) (i.e. bool type)
Operator

==
!=
>=
>
<=
<

Description
Equal to
Not equal to
Greater than or Equal to
Greater than
Less than or Equal to
Less than

Department of Computer Science

Example
a == ’a’
x != 10
y >= 100
y > 10
x <= 100
x < 10

C++ Selection Structures


Selection Criteria
Selection Statements

Conditional Expression

Logical Operators

used to create more complex conditions
its operands in bool type
also return bool value
short-circuit evaluation
Operator
&&
||
!

Description
And
Or
Not

Department of Computer Science

Example
(age < 40) && (term < 10)
(age < 40) || (term < 10)
!(age < 40)

C++ Selection Structures


Selection Criteria
Selection Statements

Conditional Expression

Operator Precedence and Associativity
List in the decreasing order of the precedence
Operator
Associativity
! unary - ++ –
Right
*/%
Left
+Left
== !=
Left
< <= > >=
Left
&&
Left
||
Left
= += -= *= /=
Right
Low
Read for more
details
Precedence
High

Department of Computer Science

C++ Selection Structures



Selection Criteria
Selection Statements
Conditional Expression

Example

char key = ’m ’ ;
i n t i = 5 , j = 7 , k = 12;
double x = 2 2 . 5 ;
Expression
i + 2 == k - 1
’a’ + 1 == ’b’
25 >= x + 1.0
k > 2 || t < 5

Equivalent
(i + 2) == (k – 1)
(’a’ + 1) == ’b’
25 >= (x + 1.0)
(k > 2) || (t < 5)

Department of Computer Science

Value
0
1
1
0


Interpretation
false
true
true
false

C++ Selection Structures


Selection Criteria
Selection Statements
Conditional Expression

Order of evaluation for Arithmetic/Relational Expression

Higher precedence operators are evaluated before lower
precedence operator
10 ∗ 2 + 2 ∗ 5 − 8 / 2 − 9 ∗ 3
20 + 10

4
− 27
30

4
− 27
26
− 27
−1


Department of Computer Science

C++ Selection Structures


Selection Criteria
Selection 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
a ! = 0 && b / a > 10
f a l s e && b / a > 10
false
a == 0 | | b / a > 10
t r u e | | b / a > 10
true

Department of Computer Science

C++ Selection Structures


Selection Criteria
Selection Statements
Conditional Expression


The bool data type
C++ uses bool for Boolean type
The actual value of true is 1 and false is o
#include < iostream >
i n t main ( )
{
bool t1 , t 2 ;
t1 = true ;
t2 = false ;
s t d : : c o u t << " The v a l u e o f t 1 i s " << t 1
<< " \ n and t h e v a l u e o f t 2 i s "
<< t 2 << e n d l ;
return 0;
}

Department of Computer Science

C++ Selection Structures


Selection Criteria
Selection Statements
Conditional Expression

If Statement
Switch Statement

If Statement


The if-else statement
directs the computer to
select a statement based
on the result of a
condition.
Syntax:

previous statement

i f ( condition )
statement 1
else
statement 2

Department of Computer Science

condition

no

yes
statement 1

C++ Selection Structures

statement 2


Selection Criteria
Selection Statements

Conditional Expression

If Statement
Switch Statement

Example
Start
Construct a C++ program
for determining income
taxes.
Assume that these taxes
are assessed at 2% of
taxable incomes less than
or equal to $20,000. For
taxable income greater
than $20,000, taxes are
2.5% of the income that
exceeds $20,000 plus a
fixed amount of $400.

Input taxable

taxable > CUTOFF

no

yes
taxes = HIGHRATE *

taxes=


(taxable - CUTOFF)

LOWRATE

+ FIXEDAMT

* taxable

Output taxes
End

Department of Computer Science

C++ Selection Structures


Selection Criteria
Selection Statements
Conditional Expression

If Statement
Switch Statement

Example

# include < iostream >
# include <iomanip >
using namespace s t d ;
const f l o a t LOWRATE = 0 . 0 2 ;

/ / lower tax r a t e
const f l o a t HIGHRATE = 0 . 0 2 5 ; / / h i g h e r t a x r a t e
const f l o a t CUTOFF = 2 0 0 0 0. 0 ; / / c u t o f f f o r low r a t e
const f l o a t FIXEDAMT = 400;
i n t main ( ) {
f l o a t taxable , taxes ;
c o u t << " Please t y p e i n t h e t a x a b l e income : " ;
c i n >> t a x a b l e ;
i f ( t a x a b l e > CUTOFF)
t a x e s = HIGHRATE∗ ( t a x a b l e −CUTOFF) +FIXEDAMT ;
else
t a x e s = LOWRATE ∗ t a x a b l e ;

Department of Computer Science

C++ Selection Structures


Selection Criteria
Selection Statements
Conditional Expression

If Statement
Switch Statement

Example
/ / set output format
c o u t << s e t i o s f l a g s ( i o s : : f i x e d )
<< s e t i o s f l a g s ( i o s : : showpoint )
<< s e t p r e c i s i o n ( 2 ) ;

c o u t << " Taxes are $ " << t a x e s << e n d l ;
return 0;
}

The results of the above program:
Please t y p e i n t h e t a x a b l e income : 10000
Taxes are $ 200

and
Please t y p e i n t h e t a x a b l e income : 30000
Taxes are $ 650

Department of Computer Science

C++ Selection Structures


Selection Criteria
Selection Statements
Conditional Expression

If Statement
Switch Statement

One-way Selection

previous statement

One-way selection =
if-no-else statement

condition

Syntax:

no

yes

i f ( condition )
statement

Department of Computer Science

statement

C++ Selection Structures


Selection Criteria
Selection Statements
Conditional Expression

If Statement
Switch Statement

Example
The following program displays an error message for the
grades that is less than 0 or more than 100
# include < iostream >
using namespace s t d ;

i n t main ( )
{
i n t grade ;
c o u t << " \ nPlease e n t e r a grade : " ;
c i n >> grade ;
i f ( grade < 0 | | grade > 100)
c o u t << " The grade i s n o t v a l i d \ n " ;
return 0;
}

Department of Computer Science

C++ Selection Structures


Selection Criteria
Selection Statements
Conditional Expression

If Statement
Switch Statement

Nested if Statement
if-statement within another if statement
i f ( c o n d i t i o n 1)
i f ( c o n d i t i o n 2)
statement 1
else
statement 2
else

statement 3

if-else chain
i f ( c o n d i t i o n 1)
statement 1
else i f ( c o n d i t i o n 2 )
statement 2
else
statement 3
Department of Computer Science

C++ Selection Structures


Selection Criteria
Selection Statements
Conditional Expression

If Statement
Switch Statement

Example
# include < iostream >
# include <cmath>
# include <iomanip >
using namespace s t d ;
i n t main ( )
{
double a , b , c , del , x1 , x2 ;
c o u t << " E n t e r t h e c o e f f i c i e n t s o f t h e e q u a t i o n : "

<< e n d l ;
c i n >> a >> b >> c ;
d e l = b∗b − 4. 0∗ a∗c ;
i f ( d e l == 0 . 0 )
{
x1 = x2 = −b / ( 2 ∗ a ) ;
c o u t << " x1 = " << x1 << setw ( 2 0 ) << " x2 = "
<< x2 << e n d l ;
}
Department of Computer Science

C++ Selection Structures


Selection Criteria
Selection Statements
Conditional Expression

If Statement
Switch Statement

Example
else i f ( d e l > 0 . 0 )
{
x1 = (−b + s q r t ( d e l ) ) / ( 2 ∗ a ) ;
x2 = (−b − s q r t ( d e l ) ) / ( 2 ∗ a ) ;
c o u t << " x1 = " << x1 << setw ( 2 0 ) << " x2 = "
<< x2 << e n d l ;
}
else

c o u t << " There i s no s o l u t i o n \ n " ;
return 0;
}

The output of the above program:
Enter the c o e f f i c i e n t s of the equation :
1
5
6
x1 = −2.0
x2 = −3.0

Department of Computer Science

C++ Selection Structures


Selection Criteria
Selection Statements
Conditional Expression

If Statement
Switch Statement

Dangling else
i f ( exp1 ) i f ( exp2 ) stmt1 else stmt2
<if-stmt>
(exp1)

if


if

else

<if-no-else>
(exp2)

stmt1

stmt2

or
<if-no-else>
if
if

(exp1)

<if-stmt>

(exp2)

stmt1

Department of Computer Science

else

C++ Selection Structures


stmt2


Selection Criteria
Selection 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
if (exp1) if (exp2) stmt1 else stmt2

Department of Computer Science

C++ Selection Structures


Selection Criteria
Selection Statements
Conditional Expression

If Statement

Switch Statement

switch Statement
switch statement
allows to execute a group of statements among multiple
groups
is based on the value of an expression
requires the expression in int type
Firstly, evaluate <expression>

Syntax:
switch ( < expression >) {
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 : <statements > ]
}
Department of Computer Science

C++ Selection Structures


Selection Criteria
Selection Statements
Conditional Expression

If Statement

Switch Statement

switch Statement
switch statement
allows to execute a group of statements among multiple
groups
is based on the value of an expression
requires the expression in int type
Syntax:

Then, compare to <value 1>

switch ( < expression >) {
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 : <statements > ]
}
Department of Computer Science

C++ Selection Structures


Selection Criteria
Selection Statements
Conditional Expression


If Statement
Switch Statement

switch Statement
switch statement
allows to execute a group of statements among multiple
groups
is based on the value of an expression
requires the expression in int type
If match,

Syntax:
switch ( < expression >) {
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 : <statements > ]
}
Department of Computer Science

execute <statements>

C++ Selection Structures


×