Home | C++ Tutorial | 4. Data Types and Handling | 1. Introduction to Data Types and Operations 2. Basic Data Types  Share This Page Introduction to Data Types and Operations

If you took algebra in school, you may recall that a name can represent a number. This idea is central to how computer languages handle data — they try to exploit a programmer's hoped-for exposure to algebra by making computer variables act like algebra variables.

Now let's create a variable and assign a value.

double x; // create

x = 80; // assign a value

These two steps can be combined:
double x = 80; // create, assign a value

Once created, a variable can be assigned:
• A number:
x = 80;

• The value of another variable:
x = y;

• The result of a mathematical operation:
x = 80.0/81.0;

• Or the value returned by a function:
x = sqrt(2.0);

This is just a short list of the ways a numeric variable can be used — there are more advanced ways, to be explained later.

Notice how I used the familiar equals sign "=" from algebra. But C++ differs from algebra. In an algebraic equation, the equals sign separates an equation into two presumably equal parts. In C++, the equals sign causes the variable to the left to be assigned the value of the variable on the right (this is called "assignment") :

double y = 5; // create variable y, assign it the number 5
double x = y // now variable x equals 5

The other use for the equals sign in C++ is as a pair: "==". This form tests whether two variables are equal. A test is a boolean operation — it results in either true or false . Here are a few C++ boolean test operators:

 x > y test: is x greater than y? x >= y test: is x greater than or equal to y? x < y test: is x less than y? x <= y test: is x less than or equal to y? x == y test: are x and y equal? x = y assignment: replace x with y

It is very important to note the difference in C++ between the assignment "=" and the equality test "==" operators. These two are always getting confused, even by experienced programmers, sometimes with mysterious results.

Now let's see how a program uses these operators. Type in some numbers and one of the operators listed above, or any other boolean or arithmetic operator, in the test boxes below: (how to deal with browser error messages)

double x = ;
double y = ;

// now, with these variables, the result of this test:

(x  y)

// is this:

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.

 Home | C++ Tutorial | 4. Data Types and Handling | 1. Introduction to Data Types and Operations 2. Basic Data Types  Share This Page