This preview shows pages 1–3. Sign up to view the full content.
:
CSci 1113
C/C++ for Scientists and Engineers
Section 010  evening class
Boolean Expressions and Flow of Control
Prof. Eric Van Wyk and Ted Kaminski
Spring 2010
c
±
Van Wyk and Kaminski
CSci 1113: C/C++ for Scientists and Engineers, evening class
Spring 2010
Page 1 / 35
Expressions: Recap
:
Exercise 3.1
Consider the following:
double d; int m, n;
d = m / n ;
How many diﬀerent ways can you modify the above code so
that
d
gets the value of
m
divided by
n
using ﬂoating point
division instead of integer division.
c
±
Van Wyk and Kaminski
CSci 1113: C/C++ for Scientists and Engineers, evening class
Spring 2010
Page 1 / 35
Expressions: Recap
:
Expressions: Recap
I
static cast: there are others that we will come to later
I
Additional modiﬁers:
short
,
unsigned
e.g.
short int x;
e.g.
unsigned int y;
c
±
Van Wyk and Kaminski
CSci 1113: C/C++ for Scientists and Engineers, evening class
Spring 2010
Page 2 / 35
Expressions: Recap
:
Representing integers
I
How should we represent negative integers?
I
Consider Two’s complement:
I
a single representation for 0
c
±
Van Wyk and Kaminski
CSci 1113: C/C++ for Scientists and Engineers, evening class
Spring 2010
Page 3 / 35
Boolean Expressions
:
Consider the following problem:
I
The City of Minneapolis needs to issue health warning if
the air pollution rises to a dangerous level.
I
Pollution measurements are taken in several locations and
measured in “number of pollutant parts per million’. An
average is computed and a health warning is raised if that
average is over 50 parts per million.
I
We know how to compute averages, but do not know how
to make decisions based on such calculations.
I
We need to compute “boolean values”  values of True or
False.
c
±
Van Wyk and Kaminski
CSci 1113: C/C++ for Scientists and Engineers, evening class
Spring 2010
Page 4 / 35
Boolean Expressions
:
Boolean?
I
Where does the work “boolean” come from?
I
George Boole  an English mathematician who discovered
a logic of true/false values.
I
This logic is part of the foundation of computer science.
c
±
Van Wyk and Kaminski
CSci 1113: C/C++ for Scientists and Engineers, evening class
Spring 2010
Page 5 / 35
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentBoolean Expressions
:
From the perspective of type systems .
..
I
We need a type for the boolean values “true” and “false”.
I
In C++ this type is written
bool
and the literals are
written
true
and
false
.
I
What operators should we have?
c
±
Van Wyk and Kaminski
CSci 1113: C/C++ for Scientists and Engineers, evening class
Spring 2010
Page 6 / 35
Boolean Expressions
Boolean Operators:
Operators
I
Relational operators:
<
,
≤
,
>
,
≥
, =,
6
=
I
Logical operators:
∧
(“and”),
∨
(“or”),
¬
(“not”)
I
In C++ these are:
<
,
<=
,
>
,
>=
,
==
,
!=
.
This is the end of the preview. Sign up
to
access the rest of the document.
 Spring '08
 Staff

Click to edit the document details