Calculations form an integral part of programming. Most of the tasks that the computer performs on our behalf are communicated to it in terms of mathematical expressions. Therefore, it is essential that we are well aware of how to manipulate numbers via arithmetic expressions in Java & C++. Let’s get started!
Arithmetic operators
There are 5 basic binary arithmetic operators offered by both programming languages. They are self-explanatory. But for the sake of completeness they are tabulated below.
Operator |
Function |
+ |
Adds the values on both sides of the operator |
_ |
Subtracts the operand on the right hand side of the operator from the one on the left hand side |
* |
Multiplies the values on both sides of the operator |
/ |
Divides the operand on the left hand side of the operator by the one on the right hand side |
% |
Divides the operand on the left hand side of the operator by the one on the right hand side and returns the remainder |
The following code snippet shows how to use these operators in C++ and Java.
1 2 3 4 5 6 7 8 9 |
int num1 = 15; int num2 = 4; int result = 0; result = num1 + num2; // result: 19 result = num1 - num2; // result: 9 result = num1 * num2; // result: 60 result = num1 / num2; // C++ result: 3, Java result: error result = num1 % num2; // result:3 |
One important thing to note is that the following statement
1 |
result = num1 / num2; |
in C++ stores 3 in the variable result instead of the correct answer 3.75. While the compiler computed the correct answer, the data type int of the variable result doesn’t allow us to store floating point values causing the implicit conversion and loss of data. Java, on the other hand, avoids such a mistake by giving you an error whenever a conversion might cause data loss. So one needs to be a bit more careful when selecting the data types of variables in C++.
Increment & Decrement Operators
Both C++ and Java inherently support increments and decrements by 1 using the following syntax:
1 2 3 |
num = 5; num++; // is equaivalent to num = num + 1 which produces 6 num--; //is equaivalent to num = num - 1 which produces 5 |
These increment operators are used very frequently in loops. (we shall study about loops in a later lesson)
Operator precedence
In a mathematical expression involving multiple operations, the precedence of the involved operators must be kept in mind. For instance what do you think the following expression should yield as a result?
1 |
result = 4 + 3 * 6; //result: 22 |
If the expression is scanned left to right the result is 42. However, since multiplication operator has a higher precedence, multiplication will be performed first followed by addition and the result will be 22. If you wish to perform addition first, you can enclose the addition operation within parenthesis.
1 |
result = (4 + 3) * 6; //result: 42 |
The precedence of arithmetic operators is depicted in the figure below. Parenthesis has a higher precedence than any of the operators.
A complete program
The following example shows Fahrenheit to Centigrade conversion in C++ and Java respectively. Note how proper brackets are necessary for correct output (212F =100C)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include<iostream> using namespace std; int main() { double f,c; f=212; c=f-32/1.8;//division operation will occur first followed by subtraction cout<<"Temperature of "<<f<<" degree Fahrenheit is equal to "<<c<<" degree Centigrade"<<endl; c=(f-32)/1.8;//subtraction operation will occur first followed by division cout<<"Temperature of "<<f<<" degree Fahrenheit is equal to "<<c<<" degree Centigrade"<<endl; } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class UseOfOperators { public static void main(String[] args) { // TODO Auto-generated method stub double f,c; f=212; c=f-32/1.8;//division operation will occur first followed by subtraction System.out.println("Temperature of "+f+" degree Fahrenheit is equal to "+c+" degree Centigrade"); c=(f-32)/1.8;//subtraction operation will occur first followed by division System.out.println("Temperature of "+f+" degree Fahrenheit is equal to "+c+" degree Centigrade"); } } |