The good news -- logical operations are easy to understand and pretty easy
write. The bad news -- they can get complex and some are difficult to get right. These notes points out half-a-dozen common mistakes (traps) made when
people are writing conditions in C++.
Reading -- Pages 45-48
| Symbol | Meaning | true example | false example |
|---|---|---|---|
| < | less than | 1<2 | 2<2 |
| <= | less than or equal | 1<=2 | 2<=1 |
| == | equal to | 1==1 | 1==2 |
| != | not equal to | 1!=2 | 1!=1 |
| > | greater than | 2>1 | 2>2 |
| >= | greater than or equal | 2>=2 | 1>=2 |
if( i = 1 ) //ERRORwhen you want to test for equality. The above compiles and runs but (1) the condition is always true and (2) it changes the variable i to be 1! Instead write
if( i == 1 )
if ( fabs(x-y) <= EPSILON )is usually what is needed -- where you choose EPSILON to be a constant small number set to an acceptable error.
const double EPSILON = 0.0000001;
| Symbol | Word |
|---|---|
| && | and |
| || | or |
| ! | not |
Hint -- Testing for divisibility
Many times a program will need to find out
if one number is divisible by another. As a simple example
is a number even or odd.
The "%" operator is useD to calculate remainders after dividing.
So we can use
to check if the number is divisible by the divisor. For example, you might write:
if( number % 2 == 0)
cout << number << "is even."<< endl;
else
cout << number << "is odd."<< endl;
Every body knows that if the year is divisible by 4 then we have a leap year:
year % 4 == 0but this is not quite right, because nowadays the rule is more complex. When the year is a century, because then we must check if the century is divisible by 4:
year % 100 == 0 and (year /100) % 4 == 0When the year is not a century we have.
year % 100 != 0 and year % 4 == 0Now we need to combine the two rules. In this case their is a leap year if either condition is true:
(year % 100 == 0 and (year /100) % 4 == 0)
or
(year % 100 != 0 and year % 4 == 0)In fact the exact rule is worse than this because the century condition was only introduced in different years depending on which country you are in... For more information check out the Wikipedia on the web.
The above shows that real life can involve complex conditions. The income
tax code provides many more complex examples. Many businesses
have very complicated formulas for calculating discounts, wages,
stocks, etc.. Analysing
and coding these is very interesting.
Hint -- lay out conditions tidily
Trap -- English syntax != C++ syntax
This is a classic error. You want to code
so you write
Sadly, in C++, this is wrong. In C++ it compiles and computes the wrong condition because of the design of C++ (see [ Strange Conditions ] below). The correct code is
(x==0) and (y==0)
Trap -- range tests
Suppose you want to test if x lies between 1 and 2 inclusive.
The following
if( ( 1 <= x ) and (x <= 2) )will work but
You have to be very careful working these out.
The data type bool and the two logical constants -- true and false
George Boole investigated "The Laws of Thought" and invented an algebra
(Boolean Algebra) that is name after him. This was an algebra with two
values -- true, false. In his honor the logical values in C++ are said
to be of type bool.
So far you have met int and double. Now you have met bool. You can have Boolean variables, and store the results of bool expressions for future use:
bool tooBig = (x >= 10000);
...
if( tooBig ) ...Introducing bool variables can simplify complex conditions and make your prgram more readable.
Add to these the rules for negating comparisons
Suppose we are working for a catelog company that has complex rules for calculating discounts depending on the country and the state. Our program may include this code
if( not (country == USA and state != AK and state!=HI))
shipping_charge = 20.00;
We can use Boolean algebra to simplify the condition:
Let
if( country!=USA or state==AK or state==HI)
shipping_charge=20.00;
if( e )the expression e is evaluated and if the value is 0 then the if treats it as false. If e is non-zero then it is taken to be true.
This is why we do not express x and y are greater than 0 like this x and y > 0. C++ treats this as (x) and (y > 0) and this is true if x is not 0 and y is greater than zero.
Similarly, C++ will convert a bool value into numbers:
and this is why 1<=x<=2 does not test if x lies betweeen 1 and 2. Instead C++ treats this as (1<=x) <= 2 -- first the program tests if x is greater than 1 and if it is produces a 1, of false then it gets a 0. These are then compared to 2. The result is therefure true!The property that in C++ data becomes bool and vice versa is exploited when we test to see if an input has worked or not:
if ( cin >> something )
{ do something }
else
{ panic }
How do you code exponents in C++
Mathematical expressions x to the power y, for general y are written
pow(x,y)using the <<cmath>> library. The mathematical exponential function e to the x can be written
exp(x)using the smae library. The parentheses are essential.
| p | q | P<q |
|---|---|---|
| false | false | false |
| false | true | true |
| true | false | false |
| true | true | false |
The word int is shorthand for integer -- the mathematician's
name for a whole(integral) number.
Are there other types we will learn about in class.
Yes: char, short, long, string, ..., arrays, vectors, ... enumerations, ...
plus the Do-It_Yourself data types: structs and classes.
When is bool used
The main use of the reserved word bool in C++ is to introduce variables
that have two possible values: true and false. Absolutely typical
is setting a flag to note something that has happened:
bool end_of_file_flag =false;
...
end_of_file_flag= (cin >> variable);
(2) The is a long long tradition of having not act on the following simple condition rather than a group:
not today and tomorrowmeans:
(not (today)) and tomorrownot:
not (today and tomorrow)
| x | y | x and y | x or y |
|---|---|---|---|
| true | true | ? | ? |
| true | false | ? | ? |
| false | true | ? | ? |
| false | false | ? | ? |