C++ operator

  • ‘C++’ supports a rich set of operators. An operator can be defined as just a symbol that tells the compiler to perform certain mathematical or logical manipulations. Taxonomy of operators
  • Assignment operators
  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Increment/decrement operators
  • Conditional operators
  • Bitwise operators.


    Expresssions

    The following are the rules regarding expressions:
  • A signed or unsigned variable name or constant is an expression.
  • An expression connected by an arithmetic operator to an unsigned variable name or an unsigned constant is an expression.
  • An expression enclosed in parenthesis is an expression.
  • Two expressions connected by an arithmetic operator is an expression.
  • Two arithmetic operators should not occur in succession in an expression.


    Assignment operators

  • Values need to be assigned to variables.
  • The ‘=’ operator is used for assignment.
  • int a =5;
  • The syntax of an assignment is,
  • Variable-name = expression;
  • For example, sum=a+b;
  • Assigns the total of contents of a and b to a variable named, sum. The variable sum appears to the left of the equal sign, which is called the assignment operator. On the left of this operator only a single variable can occur, expressions and constants are strictly not allowed. The declaration of a variable and its initial assignment is possible, as a single line. This takes the form,
  • [qualifier] type identifier=constant Or
  • [qualifier] type identifier = predefined variable
  • example: auto int no = 100;
  • C++ also permits the initialization of more than one variable in one statement using multiple assignment operation.
  • Similarly, compound assignment operators can be used.
          Eg:+=,-=,/=,%=
    
            a=a+5 or a+=5
    
            b=b-10 or b-=10
    
            c=c/2 or c/=2
    
            prod=prod * no   or prod*=no
            year=year%4      or year%=4 
    


    Arithmetic operators

    The arithmetic operators are given below

    OperatorMeaning
    +Addition
    -Substraction
    *Multiplicationtion
    /Division
    %Modulus


    Modulus operators

  • The modulus operator is represented by the % (percent symbol).
  • This returns the remainder of the division, hence 11 mod 2 is represented in ‘C++’ as, and 11 % 2 and the result is 1, the remainder of the division.
  • 10 % 5 is 0 as there is no remainder after this division.


    Symbolic Constant

  • When a numeric value or a constant is to be used in the program, then, it can be given a symbolic definition or preprocessor directives which means, before compilation every occurrence of the symbolic-name in the program is substituted by the value-of-constant as given in the syntax.
  • SYNTAX: #define symbolic-name value-of-constant
  • This name is usually written in upper case to distinguish it from an ordinary Variable Example: # define MAX_MARKS 100


    Mixed Mode

  • Having known the basic two types int and float, the next logical question is whether mixing of these two types is allowed.
  • That is, an arithmetic expression containing an operand of type int and another of type float, this is allowed.
  • The integer value is automatically converted to type float for the calculation.
  • Such automatic conversions are known as implicit conversion.


    Type Conversions

  • When two operands of different types are encountered in the same expression, the lower type variable is converted to the type of the higher type variable. These conversions take place invisible to the user.

    Relational Operators

    Logical Operators

    Increment/Decrement Operator

  • Apart from + and -, ‘C++’ provides two very useful operators. They are ++ and -- defined as increment and decrement respectively.
  • The ++ adds 1 to the operand and -- operators decrements 1 from the operand. Both are unary operators.
  • These operators can be used in two ways:
  • pre-operator (before the variable)
  • post-operator (after the variable)
    Syntax:   ++variable; --variable (pre-operator)
    	  variable++; variable-- (post-operator)
    
  • A prefix operator first adds 1 to the operand and then the result is assigned to the variable on left.
  • In contrast, a postfix operator first assigns the value to the variable on the left and then increments the operand
     
       ++variable is equivalent to
         variable=variable+1; or variable+=1;
    
         --variable is equivalent to
         variable=variable-1; or variable-=1;
    
    example:
    
          int i=5;
             a=i++;              a=++i;
               a will be 5		a will be 6
    	
    	i will be 6		i will be 6
    


    Conditional Operators(?:)

  • This is a compressed version of the if - else statement.
  • The syntax of this statement is: Expression 1 ? Expression 2 : Expression 3;
  • The operator pair works as given below:
  • The entire expression is evaluated from left to right. Expression 1 is evaluated first, if it is non zero (true) then Expression 2 is evaluated and becomes the value of the expression. If Expression 1 is false (not true) Expression 3 is evaluated and its value becomes the value of the expression. So only one of the Expressions either Expression 2 or Expression 3 is evaluated and not the both during a given evaluation.
    Example:
    #include 
    main()
    {
    int a, b, x;
    a=5;
    b=10;
    x=(a > b) ? a:b;
    printf(“ Value of x is %d\n”,x);
    }
    In the statement   c = (a > b) ? a: b;
    
  • a and b are operands.
  • If the condition is true then value of a is assigned to c otherwise b is assigned to c


    Precedence of Operators

  • In an expression, operators with higher precedence are evaluated before those of lower precedence. Operators on the same row have equal precedence and are evaluated left to right.
    Example:    2 + 3 * 2 / 2-1
        The expression is evaluated from left to right according to the rules of precedence.
    2 + 6 / 2 -1
    2 + 3 - 1
       5 - 1
        4
    Example:      3 * 2 + 3 - 1 * 2/3 + 3 % 2
        The expression is evaluated from left to right according to the rules of precedence.
       
    6 + 3 -1 * 2/ 3 + 3 % 2
       6 +  3 - 1 * 0 + 3 % 2
       6  + 3 - 0 + 3 % 2
       6 + 3 - 0  + 1
        9 - 0 + 1
        9 + 1
          10
    Example:  (2 + 3) * 2/ 2 -1
    The expression is evaluated from left to right
    according to the rules of precedence.
    5 * 2 / 2 -1
    10  2 - 1
    5 - 1
     4
    


Enumerated Data Types << Previous

Next >> Decision Statements

Our aim is to provide information to the knowledge seekers. 


comments powered by Disqus


Footer1