C++ Operators - III
The following topics are covered in this section:
3. Assignment Arithmetic Operators/Shorthand Operator(+ =, - =, * =, / =)
Assignment arithmetic operator is a combination of an arithmetic operator and the assignment operator. Consider the example:
X + = 3;
The above statement is the same as:
X = X+3;
i.e. the value of X is incremented (or increased) by 3 and the new incremented value is assigned to X. So if X had a value of 2 before this expression, then X will be 5 after this expression is executed.
Operator | Operation performed |
a+=b | a = a + b |
a-=b | a = a – b |
a*=b | a = a * b |
a/=b | a = a / b |
a%=b | a = a % b |
4. Relational Operators ( < , > , = = , ! = , >= , <= )
Relational operators are also binary operators (since they operate on two operands). They are used for comparing two values and the result of the comparison is either true (value 1) or false (value 0).
Some examples are given below:
5>4 will return a value of True (1)
2>3 will return a value of False (0)
In programs that you write, comparisons will usually be made between one variable and a constant or between two variables. For example:
x>y
z>10
> means ‘greater than’ while >= stands for ‘greater than or equal to’.
x>=y
will yield a true value even if x = y whereas x>y will yield a value of false when x = y. Be clear as to what relation you want to test when using these operators.
Suppose you want to test whether two variables are equal, you have to make use of the equality operator. The equality operator is denoted by = = (double equal to signs).
Remember: Many beginners in programming use the equality operator and assignment operator interchangeably. The assignment operator is a single ‘equal to’ sign and it is meant only for assigning values to variables. The equality operator (a double ‘equal to’ sign) is used to check whether two values are equal.
Relational Operator |
Operation Performed |
Result of Operation |
x>y |
Is x greater than y? |
True/False |
x<y |
Is x less than y? |
True/False |
x>=y |
Is x greater than or equal to y? |
True/False |
x<=y |
Is x less than or equal to y? |
True/False |
x==y |
Is x equal to y? |
True/False |
x!=y |
Is x not equal to y? |
True/False |
We’ll write a simple program for comparing two numbers and displaying the appropriate result.
#include <iostream.h> |
It might appear as if there is a white space between the two ‘equal to’ symbols used in the equality operator but this is not the case. Do not leave a blank space between the two symbols (this will produce a compile error).
We still haven’t covered the topic on ‘if’ conditions but you should be able to understand the working of the above program. The two numbers that are obtained from the user are compared using three ‘if’ conditions. Depending on which condition is satisfied, the corresponding output will be displayed.
An example for the output displayed is:
Enter the two numbers : 5 6
The number 5 is less than 6
Very frequently the above program is written with a small mistake which will lead to severe logical errors. Instead of typing
num1= = num2
within the ‘if’ condition, the following mistake is made:
if (num1 = num2) // Logical Error
{
cout<<"The number "<<num1<<" is equal to "<<num2;
}
What do you think will happen? Let us assume that the two numbers are 7 and 5.
The output will be:
Enter the two numbers : 7 5
The number 7 is greater than 5The number 5 is equal to 5
When the following code is encountered:
if (num1 = num2)
the computer assumes that this is an assignment and not a comparison (because the assignment operator has been used). Hence in this line of coding, the value of ‘num2’ will be assigned to ‘num1’ (both ‘num2’ and ‘num1’ are now 5). Now within the ‘if condition’ we have a non-zero positive value (this value is 5 and is equivalent to ‘true’). So, the compiler will execute the cout statement within that ‘if’ condition irrespective of the values of ‘num1’ and ‘num2’. This should give you a good idea as to how a small mistake can completely alter the logic of the program.
The ‘if’ statement will be dealt in Chapter 4, but for the time being just remember that whatever you type within an ‘if’ statement will be executed if the condition is true.
When testing whether a variable is equal to a constant some programmers prefer to use the following method:
if (100 = = x)
instead of the usual:
if (x = = 100)
The advantage of the first method is that even if the programmer commits the mistake:
if (100 = x)
the compiler will produce an error (since you cannot assign a value to a constant).
In the following program, we have coded to display the result of num>5. Do you think it is valid?
#include <iostream.h> |
Always remember that the result of a comparison yields a value of TRUE (1) or FALSE (0). Hence the above program is perfectly correct. In case you enter a value that is greater than 5 you will get the output as 1 else you will get 0. 0 is considered as false and all other values are considered to be true.
Copyright © 2004 Sethu Subramanian All rights reserved.