CSI 201 - Introduction to Computer Science
Chapter 7 More Flow of Control Brian R. King Instructor
3/24/2006 CSI 201 -- Chapter 07 2
Introduction
A few more details about boolean
expressions will be discussed
Complex if-else statements (which were
already covered)
If statements embedded inside if statements (see
ex6.cpp from Chapter 2 lecture notes)
The switch statement The for statement The C++ library Random Number Generator
(not covered in the book)
3/24/2006 CSI 201 -- Chapter 07 3
More boolean expressions
We covered most of section 7.1 already:
C++ type bool C++ keywords true and false How to declare and use boolean variables How to return a boolean value from a function How boolean expressions convert to values of
type int.
Let's look more at how C++ evaluates complex
boolean expressions…
3/24/2006 CSI 201 -- Chapter 07 4
Order of evaluation
- Boolean expression have an order of evaluation just like arithmetic expressions
do.
- We can create an updated list of precedence rules combining the logical and
comparison operators with the arithmetic operators.
- The precedence rules are in the following order, with the most important listed
first:
- +, -, ++, --, !
- *, /, %
- +, -
- <, >, <=, >=
- ==, !=
- &&
- ||
- Therefore, many complex boolean expressions that are a combination of both
boolean and arithmetic operators can still be evaluated:
- Example:
- if (x + 3 > 10 || y / 10 < 1) // Notice .. no parenthesis required
cout << "OK";
- Parenthesis can override the general order of evaluations.
Logical Operators Comparison Operators Arithmetic Operators Unary Operators