What are Operators in Programming?

An operator is a symbol that indicates an operation to be performed. We are familiar with operators in mathematics; operators used in computer programming are—in many ways—similar to mathematical operators.

  • Operators are used in expressions.
  • An expression is an instruction that combines one or more operands, zero or more operators, and zero or more pairs of parentheses.
  • Operands are the terms on which an operator performs an operation and gives a result.
  • So, the expression “a+b” can be broken down into the following three components:
  • a, which is the left operand
  • the plus symbol (+), which is the operator
  • b, which is the right operand

An operator in computer programming is a symbol that has a defined function. An operator tells the compiler or interpreter to perform specific mathematical, logical, or other operations—or manipulations—on the operands to arrive at a result.

How many are the Basic Operators?

Before learning about the basic operators used across computer programming languages, let’s first look at the types of operators and how they are categorized. Operator types and categories are depicted in the diagram below:

" Different types of basic operators and their classification"

The classification of operators into unary, binary and ternary is based on the number of operands an operator requires.

Unary operators operate on a single operand. The increment operator (++) and the decrement operator (--) are examples of unary operators. The table below describes these operators:

Unary operators

Unary operators operate on a single operand. The increment operator (++) and the decrement operator (--) are examples of unary operators. The table below describes these operators:

Binary operators

Binary operators operate on two operands. The addition operator (+) and the division operator (/) are examples of binary operators.

The following are the five types of binary operators used in computer programming:

  • Arithmetic
  • Logical
  • Bitwise
  • Relational
  • Assignment

Arithmetic Operators perform arithmetic operations. The table below describes the arithmetic operators:

OperatorDefinitionSyntaxOperationExample
+Additionx+yAdds the left operand and the right operandx = 5, y = 2 after c = x+y c = 7
-Subtractionx-ySubtracts the right operand from the left operandx = 5, y = 2 after c = x-y c = 3
  *  Multiplication  x*yMultiplies the left operand and the right operandx = 5, y = 2 after c = x*y c = 10
/Divisionx/yDivides the left operand by the right operandx = 6, y = 2 after c = x/y c = 3
%Modulusx%yDivides the left operand by the right operand and returns the remainderx = 5, y = 2 after c = x%y c = 1

Logical operators combine two or more conditions and return a Boolean value as the result.

Note: An exception among the logical operators is that logical NOT is a unary operator.

The table below describes the logical operators:

OperatorDefinitionSyntaxOperationExample
&&logical ANDx && yReturns true if both x and y are true, else returns falsex = true, y = true after c = x && y c = true
||logical ORx || yReturns true if either x or y is true, else returns falsex = true, y = false after c = x && y c = true
!logical NOT!xReturns true if x is false, else returns falsex = true after c = !x c = false

Relational operators compare values and return a Boolean value as the result. The table below describes the relational operators:

OperatorDefinitionSyntaxOperationExample
==equalx == yReturns true if the values of the operands are equal; returns false if the values of the operands are not equalx = 5, y = 5 after c = (x == y) c = true
!=not equalx != yReturns true if the values of the operands are not equal; returns false if the values of the operands are equalx = 5, y = 2 after c = (x != y) c = true
greater thanx > yReturns true if the value of the left operand is greater than the value of the right operand, else returns falsex = 5, y = 2 after c = (x > y) c = true
less thanx < yReturns true if the value of the left operand is less than the value of the right operand, else returns falsex = 5, y = 2 after c = (x < y) c = false
>=greater than or equalx >= yReturns true if the value of the left operand is greater than or equal to the value of the right operand, else returns falsex = 5, y = 5 after c = (x >= y) c = true
<=less than or equalx <= yReturns true if the value of the left operand is less than or equal to the value of the right operand, else returns falsex = 5, y = 2 after c = (x <= y) c = false

Bitwise operators use the binary representations of the operands and perform the indicated operation on each corresponding bit.

Note: An exception among the bitwise operators is that bitwise NOT is a unary operator.

The table below describes the bitwise operators:

OperatorDefinitionSyntaxOperationExample
&bitwise ANDx & yUses the operands’ binary values and performs the AND operation on the corresponding bitsx = 8, y = 2 after c = x & y c = 1000 & 0010 c = 0000 = 0
|bitwise ORx | yUses the operands’ binary values and performs the OR operation on the corresponding bitsx = 12, y = 2 after c = x | y c = 1100 | 0010 c = 1110 = 14
^bitwise XORx ^ yUses the operands’ binary values and performs the XOR operation on the corresponding bitsx = 8, y = 10 after c = x ^ y c = 1000 | 1010 c = 0010 = 2
<< left shiftx << yMoves the bit values of the left operand to the left by the number of bits the right operand specifies; this is the same as multiplying x by 2yx = 8, y = 2 after c = x << y c = 1000 << 0010 c = 100000 = 32
>> right shiftx >> yMoves the bit values of the left operand to the right by the number of bits the right operand specifies; this is the same as dividing x by 2yx = 8, y = 2 after c = x >> y c = 1000 >> 0010 c = 0010 = 2
~bitwise NOT~xFlips each bit of the operandx = 7 after c = ~x c = ~0111 c = 1000 = 8

Assignment operators assign the result of the specified operation to the left operand; no additional variable is used to store the result. The simple assignment operator (=) assigns the value of the right operand to the left operand. The table below describes the assignment operators:

OperatorDefinitionSyntaxOperationExample
=assignmentx = yAssigns the value of the right operand to the left operandx = 3, y = 2 after x = y x = 2
+=addition and assignmentx += yAdds the right operand to the left operand and assigns the result to the left operandx = 5, y = 2 after x += y x = x+y x = 7
-=subtraction and assignmentx -= ySubtracts the right operand from the left operand and assigns the result to the left operandx = 5, y = 2 after x -= y x = x-y x = 3
*=multiplication and assignmentx *= yMultiplies the right operand by the left operand and assigns the result to the left operandx = 5, y = 2 after x *= y x = x*y x = 10
/=division and assignmentx /= yDivides the left operand by the right operand and assigns the result to the left operandx = 6, y = 2 after x /= y x = x/y x = 3
%=modulus and assignmentx %= yTakes the modulus of the two operands and assigns the result to the left operandx = 5, y = 2 after x %= y x = x%y x = 1

Ternary operators operate on three operands. The conditional operator (?) is an example of a ternary operator. It uses the syntax (condition? option1: option2).

The conditional operator is an alternative to the if-else statement. The operator first evaluates the condition. If the condition is true, option1 will be the resultant value; if the condition is false, option2 will be the resultant value.

The following is an example of how the conditional operator can be used:

Suppose we need to find the larger number from among a and b.

Consider the expression num = (a > b) ? a : b

Here, num is the variable that will store the larger number from among a and b.

The expression can be evaluated thus: If a is greater than b, then num = a else num = b.

Operator Precedence

In computer programming, knowledge of operator precedence and associative rules are essential. Operator precedence is similar to the PEMDAS rule in mathematics.

When two operators with different precedence are adjacent to each other in an expression, which operator should be evaluated first is based on the operator precedence rule.

When two operators with the same precedence are adjacent to each other in an expression, which operator should be evaluated first is based on associative rules—which tell the compiler whether it should evaluate from left to right (LR) or from right to left (RL).

The following table shows operator precedence and lists associative rules. The higher in the table an operator is, the higher its precedence.

DefinitionOperator(s)Associativity
Post-increment, post-decrement++, --LR
Pre-increment, pre-decrement++, --RL
Logical NOT, Bitwise NOT!RL
Multiplication, Division, Modulus*, /, %LR
Addition, Subtraction+, -LR
Shift>>, <<LR
Relational less than, less than or equal to<, <=LR
Relational greater than, greater than or equal to>, >=LR
Equality==, !=LR
Bitwise AND&LR
Bitwise XOR^LR
Bitwise OR|LR
Logical AND&&LR
Logical OR||LR
Conditional?:RL
Assignment=, *=, /=, %=, +=, -=RL

Context and Applications

This topic is significant for learning basics of programming and it is also important in professional exams for undergraduate courses especially for:

  • Bachelor of Technology in Computer science
  • Bachelor of Technology in Computer engineering
  • Bachelor of Technology in Information Technology
  • Bachelor of Engineering in Computer science
  • Bachelor of Engineering in Computer engineering
  • Bachelor of Engineering in Information Technology
  • Bachelor of Computer Applications (BCA)

Want more help with your computer science homework?

We've got you covered with step-by-step solutions to millions of textbook problems, subject matter experts on standby 24/7 when you're stumped, and more.
Check out a sample computer science Q&A solution here!

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science

Programming

Fundamentals of Programming

Operators

Basic Operators Homework Questions from Fellow Students

Browse our recently answered Basic Operators homework questions.

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science

Programming

Fundamentals of Programming

Operators