c sharp Logo

Operators


Arithmetic operators

Arithmetic operators are used to perform arithmetic operations on data. The most common arithmetic operators in C++ are:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Modulus (remainder of a division)

For example, the following code adds two numbers and stores the result in the variable sum:

int sum = 10 + 20;

 

The following code subtracts two numbers and stores the result in the variable difference:

int difference = 20 - 10;

 

The following code multiplies two numbers and stores the result in the variable product:

int product = 10 * 20;

 

The following code divides two numbers and stores the result in the variable quotient:

int quotient = 20 / 10;

 

The following code finds the modulus of two numbers and stores the result in the variable modulus:

int modulus = 20 % 10;

 

Assignment operators

Assignment operators are used to assign values to variables. The most common assignment operator in C++ is the = operator.

For example, the following code assigns the value 10 to the variable x:

int x = 10;

 

You can also use compound assignment operators to perform arithmetic operations and assign the result to a variable. For example, the following code adds 1 to the variable x:

x++;

 

The following code subtracts 1 from the variable x:

x--;

 

Comparison operators

Comparison operators are used to compare two values and return a Boolean value (true or false). The most common comparison operators in C++ are:

  • ==: Equal to
  • !=: Not equal to
  • <: Less than
  • >: Greater than
  • <=: Less than or equal to
  • >=: Greater than or equal to

For example, the following code compares two numbers and stores the result in the variable result:

bool result = 10 == 20;

 

The variable result will be equal to false because 10 is not equal to 20.

Logical operators

Logical operators are used to combine two Boolean values and return a Boolean value. The most common logical operators in C++ are:

  • &&: Logical AND
  • ||: Logical OR
  • !: Logical NOT

The logical AND operator returns true if both of its operands are true, and false otherwise. The logical OR operator returns true if either of its operands are true, and false otherwise. The logical NOT operator returns true if its operand is false, and false otherwise.

For example, the following code combines two Boolean values using the logical AND operator and stores the result in the variable result:

bool result = 10 == 20 && 20 == 30;

 

The variable result will be equal to false because both operands of the logical AND operator must be true for the result to be true.

The following code combines two Boolean values using the logical OR operator and stores the result in the variable result:

bool result = 10 == 20 || 20 == 30;

 

The variable result will be equal to true because either operand of the logical OR operator must be true for the result to be true.

The following code negates a Boolean value using the logical NOT operator and stores the result in the variable result:

bool result = !(10 == 20);

 

The variable result will be equal to true because the operand of the logical NOT operator is false.

Examples

The following example shows how to use arithmetic, assignment, comparison, and logical operators in C++:

#include <iostream>

 

int main() {

  // Declare variables.

  int x = 10;

  int y = 20;

 

  // Perform arithmetic operations.

  int sum = x + y;

  int difference = x - y;

  int product = x * y;

  int quotient = x / y;

  int modulus = x % y;

 

  // Assign values to variables.

  x++;

  y--;

 

  // Compare two values.

  bool result = x == y