Sunday, July 18, 2010

C++: An overview of operators

Suppose if we were asked to create a temperature converter that will convert
between Fahrenheit (degrees F) and Celcius (degrees C). The formula is:
Degrees F = -40 + (1.8 * degrees C)
Degrees C = -40 + (0.45 times degrees F)
So, zero degrees C would be 32 degrees F, and 50 degrees F would be 10
degrees C. But how do we express this in C++?

The reason why I ask this question is to bring two important concepts:
first, C++ offers a vast number of operators that can help us to
calculations and so forth, a second, it is a best practice to write our
objectives in something we can understand before venturing out into C++ code
world.--Source: O'Reilly Books. "Practical C++ Programming". Let us go into
these concepts in more detail.

C++ offers a vast number of operators, such as addition, subtraction and
comparison operators. Most of them comes from mathematics, such as +
(addition or plus), - (minus or dash), * (multiply) and / (divide or slash).
Also, the useful comparison operators are less-than (<), greater-than (>)
and variants of these, which are less-than or equal to (<=) and greater-than
or equal to (>=. In addition to these ones, there are anumber of "special"
ones, including ++ (increment or add by one), -- (double dash or subtract by
one) and == (double equals) - the double equals requires a section of it's
own to go over what it is. I'll go over those in sections below.

The math symbols - +, -, *, / - are used for calculations or similar
operations. For example:
int total-distance; total_distance = start_distance + added_distance;
We are working with an odometer program. The total distance would be the
distance where we've started calculating the walking distance (the start
variable) and the length of our walk (additional distance) added together.

Another example to calculate wall length from number of bricks and their
length:
int wall_length; wall_length = brick_length * number_of_bricks;
Ah, we are writing a construction calculator to calculate wall lengths
(handy for contractors). So, in the expression, we are multiplying the
number of bricks by their length to get the total length of a wall.

A more complicated one - a program that verifies if a given number is even
or not:
Int number; int dividant;
Bool is_even_number = true;
Number % dividant == 0;
Is_even_number = false;
Number % dividant == 1;
I see where you might be confused - the % (percent), or modulus operator.
When this operator is used, the program will return the remainder after a
division e.g. the remainder of 25/4 would be 1. Also, you might have seen
the Boolean operator, but I believe this is the first time you are seeing
this variable type in action.

The last example also brings an important note: the difference between "="
and "==. The "equals" by itself is the assignment operator where a value is
assigned to a variable. This one is read as "some variable is assigned to a
value." The two equals sign is simply the equivalent of saying that
something on the left IS the same as something on the right (there is a
counter operator that says something on the left IS NOT equal to something
on the right). For instance, check out the difference between the following
code:
int number = 9+1;
versus
10 == 9+1;
The first code would be read as, 'the integer variable, number, is assigned
to addition of 9 and 1." As opposed to this, the second expression would be
read as, "10 is 9 plus 1."

As opposed to these scenarios, the "counter operator' (!=, read as "not
equal to") in action (based on even/odd number verifier) would be:
Bool is_odd_number = true;
number % dividant != 0;
This is read as, "the given number is odd when the remainder of number
divided by a dividant is not zero."

I think that covers almost all operators. The reason why I left out
comparison operators is because of a topic that's coming up next: how to put
conditions so that a computer can make decisions...

2 comments:

  1. Hey, could you put in a part where you explain about compiling the code? It may seem irrelevant if you use VC++, but people who use other compilers and developer tools may not know how to do so.

    At least people who use VC++ only need to build their program straight from VC++. Oh, you might also want to say how they can view their program. =)

    ReplyDelete
  2. Hi, I see. Thanks for reminding me about that. I'll write a small tip today about that...

    ReplyDelete