Operators in c++


C++ provides a rich operator environment. An operator is a symbol that tells the compiler to perform a specific mathematical or logical manipulation.

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Compound Assignments
  • Increment / Decrement Operators

Arithmetic Operators

  • C++ defines the following arithmetic operators:

  • Operator Description
    + Addition
    - Subtraction
    * Multiplication
    / Division
    % Modulus
    ++ Increment
    -- Decrement

 

Relational  Operators

  • Relational operators evaluate values on the left and right side of the operator and return the relation as either True or False.

  • Operator Description
    == is equal to
    < is less than
    > is greater than
    <= is less than or equal to
    >= is greater than or equal to
    != is not equal to

 

Logical Operators

  • The logical operators are used to support the basic logical operations AND, OR, and NOT.
  • Operator Description
    && AND
    || OR
    ! NOT

 

Assignment Operators

  • These operators are useful when assigning values to variables.

  • var = expression;
    
    int x, y, z;
    x = y = z = 100; // set x, y, and z to 100

 

Compound Assignments

  • C++ provides special compound assignment operators that simplify the coding of certain assignment statements. Let’s begin with an example.
  • The assignment statement shown here:

    x = x + 10;
    
    //can be written using a compound assignment as
    
     x += 10;

Increment / Decrement Operators

  • The increment operator ++ modifies the operand by adding 1 to its value and cannot be used with constants for this reason.
  • Given that i is a variable, both i++ (postfix notation) and ++i (prefix notation) raise the value of i by 1. In both cases the operation i=i+1 is performed.

  • ++i i is incremented first and the new value of i is then applied,
    i++ the original value of i is applied before i is incremented.