Comparison and arithmetic Operators in C++
C++ Taking the Bull by the Horns – Part 9
Forward: In this part of the series, we talk about some common C++ Operators
By: Chrysanthus Date Published: 22 Aug 2012
Introduction
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 defined 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.
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 <iostream>
using namespace std;
int main()
{
int myIdent = 25;
int hisIdent = 30;
if (myIdent != hisIdent)
{
cout << "The values of the two objects are not equal.";
}
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 <iostream>
using namespace std;
int main()
{
int myIdent = 50;
int hisIdent = 50;
if (myIdent != hisIdent)
{
cout << "The values of the two objects are not equal.";
}
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 <iostream>
using namespace std;
int main()
{
int ident1 = 60;
int ident2 = 70;
if (ident2 > ident1)
{
cout << "The value of ident2 is greater than the value of ident1.";
}
return 0;
}
Read and try the above code.
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, <= . It returns true if the left operand is less than or equal to the right operand.
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 <iostream>
using namespace std;
int main()
{
int id1 = 20;
int id2 = 30;
int id3 = id2 + id1;
cout << id3;
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 defined 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 <iostream>
using namespace std;
int main()
{
int id1 = 20;
int id2 = 30;
int id3 = id2 - id1;
cout << id3;
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 defined 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 <iostream>
using namespace std;
int main()
{
int id1 = 20;
int id2 = 30;
int id3 = id2 * id1;
cout << id3;
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 defined 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.
Read and try the following code. The explanation is given below:
Code example:
#include <iostream>
using namespace std;
int main()
{
int id1 = 3;
int id2 = 15;
int id3 = id2 / id1;
cout << id3;
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 defined identifier, id3. This division is done without affecting or changing the contents of id2 and id1. The division 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 <iostream>
using namespace std;
int main()
{
int id1 = 17;
int id2 = 12;
int id3 = id1 % id2;
cout << id3;
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 <iostream>
using namespace std;
int main()
{
int id1 = 10;
int id2 = ++id1;
cout << id2;
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 <iostream>
using namespace std;
int main()
{
int id1 = 10;
int id2 = id1++;
cout << id2;
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.
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 <iostream>
using namespace std;
int main()
{
int id1 = 15;
int id2 = -id1;
cout << id2;
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
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT