Notice that the operator "=" simply assigns y's value to x, but "==" tests to
see if x and y are equal.
Here are some additional operators for you to try out — let's see if you can
guess how they work:
!=
|
not-equal
|
&
|
bitwise-and
|
&&
|
logical-and
|
|
|
bitwise-or
|
||
|
logical-or
|
^
|
exclusive-or
|
>>
|
bitwise-right-shift
|
<<
|
bitwise-left-shift
|
Notice that the bitwise-left-shift operator "<<" is identical to the
stream insertion operator from the previous section. This same-symbol,
different-purpose problem, resulting from an unfortunate choice made by the
designers of C++, is another possible source of confusion. The compiler
resolves this problem using something called "
context.
"
In the context of a stream
, "<<" means, "Insert something into the stream."
In the context of ordinary variables
, "<<" means, "Shift the left-hand variable's bits to the left (toward
more significant bits) by the number of places given by the variable on the
right."
Context is an important C++ concept, rather advanced, and one I will be
expanding on later. I only bring it up now because of the very likely
beginner's confusion about the different uses of "=" in algebra and C++, the
confusing use of "=" and "==" in C++, and the different uses of "<<" in
C++.
You need to realize I created the above on-line example program, not in C++,
but in JavaScript (a computer language that can be run interactively within a
Web page). Although it has the same boolean test operators as C++, JavaScript
is not the same as C++, and some of the things that you can do above, you cannot do
in C++. Here is a compilable C++ example of a program that will allow you to
perform many of the same tests — but often with different error messages and outcomes:
#include <iostream>
using namespace std;
int main()
{
double x = 1;
double y = 2;
cout << "result: " << (x == y) << endl;
return 0;
}
Compile this program after placing your choice of boolean operator in the test
segment "(x == y)". Notice the differences in the compiled C++ version. As you
try out different operators, your compiler will tell you some of the operators
cannot be applied to double variables.
You will also notice the C++ program reports "true" as 1, "false" as 0.
In C++, any nonzero value counts as "true,"
while zero is "false." This expression --
while(1) {
cout << "Bart Simpson is not the name of a world conqueror." << endl;
}
— will run forever, because the expression "(1)" is true, and it is
always
true. The test is entirely meaningless, because the thing being tested is a
constant that evaluates as "true." The program that contains this expression
will never stop on its own.
Note: If you see lots of browser error messages
While using this interactive programming page, if you are running Microsoft
Internet Explorer, be sure to turn off "script
debugging" so you won't have to look at endless message dialogs about errors
you may have intended to make. Go to Tools ... Internet Options ... Advanced
... Browsing ... Disable Script Debugging.