This is part 9 of my series, C++ Taking the Bull by the Horns. In this part of the series, we talk about some common C++ Operators. We have seen the logical operators. We have also seen the assignment (=) and equal (==) operators. In this part we look at comparison and arithmetic operators. Remember, take things the way I give you in this tutorials. Do not in your mind, add or subtract anything to what is in these tutorials. You can only do that after you have completed the tutorials.
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
Operand
An Operand is an identifier or a literal (value) associated with an operator. Consider,
myIdent = 30; //this ID was declared as an int
myIdent is a left operand and 30 is a right operand. = is the assignment operator, not the equal operator. The equal operator is, == and is used only in conditions.
Consider:
myIdent && hisIdent && herIdent
There are three operands in the above expression. So, you can talk of the first, second and third operand.
Comparison Operators
A comparison operator compares the operands on its sides and returns a logical value depending on whether the comparison is correct or wrong. If the comparison is correct a logical value of true is returned. If it is wrong, a logical value of false is returned. Another name for Boolean Value is Logical Value, which is either true or false. In C++, true is represented by 1 and false is represented by 0.
The Equal Operator
We have seen this before. It is ==, typed as a double assignment operator. The equal operator returns true if operands are equal, otherwise it returns false. We have seen many examples of this.
The Not Equal Operator
The Not Equal operator is the opposite of the Equal Operator. The Not Equal operator is, != . It returns true if the operands are not equal, otherwise it returns false. Let us look at some examples:
Try the following code:
#include
using namespace std;
int main()
{
int myIdent = 25;
int hisIdent = 30;
if (myIdent != hisIdent)
{
cout }
return 0;
}
myIdent is 25, hisIdent is 30. The condition is read like this: If myIdent is not equal to hisIdent, then the if-block will be executed. Since the values of the objects are not equal, (myIdent != myIdent) returns true.
In the following code, the values of the two objects are equal, so the condition returns false and the if-block is not executed.
#include
using namespace std;
int main()
{
int myIdent = 50;
int hisIdent = 50;
if (myIdent != hisIdent)
{
cout }
return 0;
}
Note: The letter O and the digit zero are not the same things. If you type the letter O in place of zero you will not have the right results. The digit zero is found in the number keypad of your keyboard. The letter O is found in the main keyboard area.
The Greater Than Operator
The Greater Than operator is, > . It returns true if the left operand is greater than the right operand. In the following example, the left operand is greater than the right operand. So the if-block is executed:
#include
using namespace std;
int main()
{
int ident1 = 60;
int ident2 = 70;
if (ident2 > ident1)
{
cout }
return 0;
}
Read and try the above code.
Greater Than Or Equal - Operator
The Greater Than or Equal operator is, >= (it is the math greater than sign followed by the math equal sign). It returns true if the left operand is greater than or equal to the right operand.
The Less Than Operator
The Less Than Operator is < .It returns true if the left operand is less than the right operand.
The Less Than or Equal - Operator
The Less than or Equal operator is,
Arithmetic Operators
Remember I said you should avoid making analogy between C++ and mathematics (arithmetic). So you should see what I have from this section to the end of this tutorial as an application of C++ and not C++ in itself. An Arithmetic operator takes one or two numbers as operands (either literals or identifiers) and returns the answer, similar to what happens in arithmetic, but not exactly, especially when you are dealing with floats. See what I have below as application of C++, not C++ in itself.
The standard arithmetic operators are addition (+), subtraction (-), multiplication (*), and division (/).
Addition Operator
Read and try the following code. The explanation is given below:
#include
using namespace std;
int main()
{
int id1 = 20;
int id2 = 30;
int id3 = id2 + id1;
cout
return 0;
}
20 is kept in the object identified by id1. 30 is kept in the object identified by id2. In the third statement of the block of the main function, C++ takes the content of id2 and adds it to the content of id1, then it puts the result as content for the object of the newly declared identifier, id3. This addition is done without affecting or changing the contents of id2 and id1. The addition is done in a different area of memory.
Subtraction Operator
Read and try the following code. The explanation is given below:
Code example:
#include
using namespace std;
int main()
{
int id1 = 20;
int id2 = 30;
int id3 = id2 - id1;
cout
return 0;
}
20 is kept in the object identified by id1. 30 is kept in the object identified by id2. In the third statement of the block of the main function, C++ takes the content of id2 and subtracts from it the content of id1, then it puts the result as content for the object of the newly declared identifier, id3. This subtraction is done without affecting or changing the contents of id2 and id1. The subtraction is done in a different area of memory.
Multiplication Operator
Read and try the following code. The explanation is given below:
#include
using namespace std;
int main()
{
int id1 = 20;
int id2 = 30;
int id3 = id2 * id1;
cout
return 0;
}
Note that the multiplication operator is * and not X. Here * is the multiplication operator and not the dereference operator. 20 is kept in the object identified by id1. 30 is kept in the object identified by id2. In the third statement of the block of the main function, C++ takes the content of id2 and multiplies it with the content of id1, then it puts the result as content for the object of the newly declared identifier, id3. This multiplication is done without affecting or changing the contents of id2 and id1. The multiplication is done in a different area of memory.
Division Operator
Read and try the following code. The explanation is given below:
Code example:
#include
using namespace std;
int main()
{
int id1 = 3;
int id2 = 15;
int id3 = id2 / id1;
cout
return 0;
}
Note that the division operator is, / . 3 is kept in the object identified by id1. 15 is kept in the object identified by id2. In the third statement of the block of the main function, C++ takes the content of id2 and divides it by the content of id1, then it puts the result as content for the object of the newly declared identifier, id3. This multiplication is done without affecting or changing the contents of id2 and id1. The multiplication is done in a different area of memory.
Other operators are the Modulus (%), Increment (++), Decrement (--), and the Negation operators. You have to learn the particular way in which each of these operators behaves.
Modulus Operator
The modulus operator divides the first operand by the second operand and returns the remainder. Read and try the following code:
#include
using namespace std;
int main()
{
int id1 = 17;
int id2 = 12;
int id3 = id1 % id2;
cout
return 0;
}
The Modulus operator is the percentage sign. For the third statement in the main function block above, C++ takes the content of id1 and divides it by the content of id2. The remainder of the division is put in the object of id3. The division is done in a different area of memory.
Increment Operator
The Increment Operator is, ++. It works with one operand, not two as the others. The operand has to be a number. When it is placed in front (prefix) of the operand, it behaves in one way. When it is placed after (postfix) the operand it behaves in another way.
Prefix: When it is prefix, it adds 1 to the operand and returns the incremented operand value. Read and try the following code:
#include
using namespace std;
int main()
{
int id1 = 10;
int id2 = ++id1;
cout
return 0;
}
In the code, initially, 10 is assigned to the object of id1. Then we have a statement. In the statement you have a new identifier, id2, the assignment operator and then "++id1". What interest us here is "++id2", where the increment operator is in front of the identifier. The value the increment operator returns is assigned to the object of id2. If you have tried the code, you would have noticed that the value of id2 is 11. This means, if used prefix, it increments the operand and then returns the incremented content of the operand object.
Postfix: When it is postfix, it returns the operand value before adding 1 to it. The returned value is the original value of the operand. The increased value is the new value of the operand, which is not returned. Read and try the following code.
#include
using namespace std;
int main()
{
int id1 = 10;
int id2 = id1++;
cout
return 0;
}
If you have tried the above code, you would have noticed that the value for id2 is 10 and the final value for id1 is 11, confirming that the incrementing took place after the value was returned.
Decrement Operator
The Decrement operator, -- , behaves like the increment operator with the only difference that it subtracts 1 instead of adding.
Negation Operator
This operator is the negative sign, - . It works with one operand (on its right); it negates the operand like in math. Read and try the following:
#include
using namespace std;
int main()
{
int id1 = 15;
int id2 = -id1;
cout
return 0;
}
Adding Value to an Object
Imagine that you have an int object already having a value. You can add some other value to the object as follows:
ident = ident + newValue;
e.g.
myObj = myObj + 5;
You can modify other objects in this way using the other arithmetic operators.
We have come to the end of this part of the series. Rendezvous in the next part.
Chrys
To arrive at any of the parts of this series, just type the corresponding title below in the Search Box of this page and click Search (you can also use any available links):
C++ Taking the Bull by the Horns - Part 1
C++ Taking the Bull by the Horns - Part 2
C++ Taking the Bull by the Horns - Part 3
C++ Taking the Bull by the Horns - Part 4
C++ Taking the Bull by the Horns - Part 5
C++ Taking the Bull by the Horns - Part 6
C++ Taking the Bull by the Horns - Part 7
C++ Taking the Bull by the Horns - Part 8
C++ Taking the Bull by the Horns - Part 9
C++ Taking the Bull by the Horns - Part 10
C++ Taking the Bull by the Horns - Part 11
C++ Taking the Bull by the Horns - Part 12
C++ Taking the Bull by the Horns - Part 13
C++ Taking the Bull by the Horns - Part 14
C++ Taking the Bull by the Horns - Part 15
C++ Taking the Bull by the Horns - Part 16
C++ Taking the Bull by the Horns - Part 17
C++ Taking the Bull by the Horns - Part 18
C++ Taking the Bull by the Horns - Part 19
C++ Taking the Bull by the Horns - Part 20
C++ Taking the Bull by the Horns - Part 21
C++ Taking the Bull by the Horns - Part 22
C++ Taking the Bull by the Horns - Part 23
C++ Taking the Bull by the Horns - Part 24
C++ Taking the Bull by the Horns - Part 25
Published by Chrys Forcha
I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a... View profile
Gangsters Take to the InternetGangsters aren't just on the streets anymore, now they are on myspace and could be soliciting your child.- Profiting from the Current Market TurmoilHow to make money from fluctuations in the stock market, the falling U.S. Dollar and the skyrocketing price of oil and gold.
- Failure of the Too Big to FailThe Federal Reserve System has an estranged relationship with the US Congress.
- Making the Best of Being Away from Home for the HolidaysYour not at home during the Holidays. How can you make this Holiday one to remember for years to come!
- The Bottom Ten/NCAA Week 5A hilarious ranking of the best of the worst college football teams!
- Comparison and Arithmetic Operators in C
- Comparison and Arithmetic Operators
- PHP Comparison and Arithmetic Operators
- ActivePerl Comparison and Arithmetic Operators
- C++ Object Identifiers
- C++ Pointers
- How to Survive the Office Jungle



