Skip to main content

Student Portal > Contests > 

Programming Contest Central

   
Welcome students Resources Competition ProfilesFAQ
  Contest calendar
  Problem of the month
  Past problems
  Games and brainteasers
  Successes
  Tutorials

Operators
Arithmetic Operators
Increment and Decrement Operators
Assignment Operators
The dot Operator
The new Operator
Relational and Logical Operators
Conditional Operators
Operator Precedence

Operators are symbols that perform special operations in programming. There are many different types of operators. The following chart illustrates these operators and their functions. Provided below is further explanation on how to use the different types of operators.


Arithmetic Operators

Operator Action

Examples
(int m=6; int n=2; int p=4; int ans;)

+ Addition operator, adds two values ans = m + n;
//ans = 8
- Subtraction operator, subtracts the second value from the first value ans = m - n;
//ans = 4
* Multiplication operator, multiplies two values ans = 3 * 4;
//ans = 12
/ Division operator, divides the first value by the second value ans = m / n;
//ans = 3
% Modulus operator, takes the remainder after simple division ans = m % n;
//ans = 0

ans = m % p;
//ans = 2


Increment and Decrement Operators

Operator Action

Examples
(int m=6; int ans;)

++ adds 1 to a variable m++;
//m = 7
This is equivalent to m = m +1 ;

ans = m++;
//ans = 6, m = 7
This increments the value of m AFTER it is used, therefore the value of ans is 6 and m is 7 . This is known as a postfix operator.

ans = ++m;
//ans = 7, m = 7
This increments the value of m BEFORE it is used, therefore the value of both ans and m is 7. This is known as prefix.
-- subtracts 1 from a variable


Assignment Operators

Operator Action

Examples
(int m=6; int n=2; int ans;)

= assignment operator ans = m;
//ans = 6
+= adds the second value to the first variable m += 4;
//m = 10
This is equivalent to m = m + 4;

m+=n;
//m=8
-= subtracts the second value from the first variable

m -= 4;
//m = 2
This is equivalent to m = m - 4;

m -= n;
//m = 4

*= multiplies the second value with the first variable

m *= 4;
//m=24
This is equivalent to m = m * 4;

m *= n;
//m = 12

/= divides the first variable by the second value

m /= 4;
m = 1;
This is equivalent to m = m / 4; (Remember that int variables truncate decimals)

m /= n;
//m = 3

%= performs modulo division on the first variable using the second value

m %= 4;
//m = 2
This equivalent to m = m % 4;

m /= n;
//m = 0



Dot Operator

Operator Action

Examples

. calls a method of a class

circle.area();
This calls the area method of the Circle class to calculate the area of a circle.



New Operator

Operator Action

Examples
(int m=6; int n=2; int ans;)

new creates an new array or object of a class

int [] numbers = new int [10];
This creates a new array called numbers.
Circle c = new Circle();
This creates a new instance of the Circle object.



Relational and Logical Operators


All the following conditions return a boolean answer (true or false).

Operator Action

Example
(int m=6; int n=2; boolean flag=true;)

> greater than

if (m > n) { ... }
The value of the conditional statement (m>n) is true.

>= greater than or equal to
< less than
<= less than or equal to
== equal

if (m == n) { ... }
The value of the conditional statement (m == n) is false.

boolean x = (m != n);
The value of x becomes the value of the conditional statement (m != n), which is true.

!= not equal
! not; inverts the value of a boolean if (!flag)
...

This means that if flag is false, then...

Note: the "=" operator means assigning a value to a variable, while the "==" operator is comparing if one value is equal to another value.


Conditional Operators

Operator Action Example
&& and If (m==1 && n==2)
...

This means that if both conditions are true (m=1 and n=2) then...
|| or If (m==1 || n==2)
...

This means that if either condition is true or both are true (m=1 or n=2 or both) then...


Operator Precedence

When there are a few operators written in the same line of code, some operators are considered before others. That is what is called operator precedence. The operator precedence from highest to lowest is:

--/++(postfix), --/++(prefix), !, /, *, %, +, -, <, <=, >, >=, ==, !=, &&, ||



There are a couple more operators that we haven't discussed. Although they may not be strictly neccessary for your purposes, you should be aware of their existance. For more information on these operators, check out some useful links:

Bitwise and Bit Shift Operators Sun Microsystem's explanation of Bitwise and Bit Shift operators.
Bitwise Operators One of the best explanations of bitwise operators.
Operators More about operators.






Back to top


Quick links

Student forum

Teacher resources

RSS Feed