Thứ Ba, 21 tháng 1, 2014

C++ Basics - More Flow of Control

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3.1
Using Boolean Expressions
Slide 3- 6
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Using Boolean Expressions

A Boolean Expression is an expression that is
either true or false

Boolean expressions are evaluated using
relational operations such as

= = , < , and >= which produce a boolean value

and boolean operations such as

&&, | |, and ! which also produce a boolean value

Type bool allows declaration of variables that
carry the value true or false
Slide 3- 7
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Boolean expressions are evaluated using values
from the Truth Tables in

For example, if y is 8, the expression
!( ( y < 3) | | ( y > 7) )
is evaluated in the following sequence
Display 3.1
! ( false | | true )
! ( true )
false
Evaluating Boolean Expressions
Slide 3- 8
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Order of Precedence

If parenthesis are omitted from boolean
expressions, the default precedence of
operations is:

Perform ! operations first

Perform relational operations such as < next

Perform && operations next

Perform | | operations last
Slide 3- 9
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Display 3.2
Precedence Rules

Items in expressions are grouped by precedence
rules for arithmetic and boolean operators

Operators with higher precedence are
performed first

Binary operators with equal precedence are
performed left to right

Unary operators of equal precedence are
performed right to left
Slide 3- 10
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Precedence Rule Example

The expression
(x+1) > 2 | | (x + 1) < -3
is equivalent to
( (x + 1) > 2) | | ( ( x + 1) < -3)

Because > and < have higher precedence than | |

and is also equivalent to
x + 1 > 2 | | x + 1 < - 3
Slide 3- 11
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Evaluating x + 1 > 2 | | x + 1 < - 3

Using the precedence rules of Display 3.2

First apply the unary –

Next apply the +'s

Now apply the > and <

Finally do the | |
Slide 3- 12
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Short-Circuit Evaluation

Some boolean expressions do not need to be
completely evaluated

if x is negative, the value of the expression
(x >= 0) && ( y > 1)
can be determined by evaluating only (x >= 0)

C++ uses short-circuit evaluation

If the value of the leftmost sub-expression
determines the final value of the expression, the rest
of the expression is not evaluated
Slide 3- 13
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Using Short-Circuit Evaluation

Short-circuit evaluation can be used to prevent
run time errors

Consider this if-statement
if ((kids != 0) && (pieces / kids >= 2) )
cout << "Each child may have two pieces!";

If the value of kids is zero, short-circuit evaluation
prevents evaluation of (pieces / 0 >= 2)

Division by zero causes a run-time error
Slide 3- 14
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Type bool and Type int

C++ can use integers as if they were Boolean
values

Any non-zero number (typically 1) is true

0 (zero) is false

Không có nhận xét nào:

Đăng nhận xét