ЁЯУЪ

C++ Operators

Jul 12, 2024

C++ Operators

Overview

  • Operators: Used frequently in code, also important for MCQ rounds in company selection processes.
  • Topics: Operators, precedence, and associativity.
  • Approach: Understand each operator with code examples. Practice along for better understanding.
  • Categories: Divided into arithmetic, relational, logical, assignment, bitwise, and miscellaneous.

Arithmetic Operators

  • Basic operators: Addition, Subtraction, Multiplication, Division, Modulus.
  • Important to consider the resulting data type of operations.
  • Examples:
    • Addition: a + b
    • Subtraction: a - b
    • Multiplication: a * b
    • Division: a / b (result type may vary)
    • Modulus: a % b (gives remainder)*

Relational Operators

  • Compare values and return true or false.
  • Types:
    • == (Equals to)
    • != (Not equals to)
    • > (Greater than)
    • < (Less than)
    • >= (Greater than or equal to)
    • <= (Less than or equal to)
  • Example: a == b, returns true if a is equal to b.

Logical Operators

  • Used to combine expressions.
  • Types:
    • && (Logical AND): True if both expressions are true.
    • || (Logical OR): True if at least one expression is true.
    • ! (Logical NOT): True if the expression is false.
  • Example: a && b

Assignment Operators

  • Used to assign values.
  • Shorthand operators:
    • +=: a += b is same as a = a + b
    • -=: a -= b
    • *=: a *= b
    • /=: a /= b
    • %=: a %= b
  • Example: a += 1

Bitwise Operators

  • Operate on binary representations.
  • Types:
    • & (AND)
    • | (OR)
    • ^ (XOR)
    • ~ (NOT)
    • << (Left shift)
    • >> (Right shift)
  • Binary Representation: Conversion between binary and decimal.
  • Example:
    • AND: a & b
    • OR: a | b
    • XOR: a ^ b
    • Left Shift: a << b (shifts bits left)
    • Right Shift: a >> b (shifts bits right)

Miscellaneous Operators

  • sizeof: Returns the size of a variable.
  • Ternary (?:): Conditional expression.
    • Syntax: condition ? expression1 : expression2
    • Example: a = (b > c) ? b : c
  • Comma (,): Used to separate expressions.
  • Pointer Operators: * (dereferencing), & (address of).
  • DOT (.) and Arrow (->): Used in class and pointer contexts.*

Unary Operators

  • Operate on single operands.
  • Types:
    • + (Unary Plus)
    • - (Unary Minus)
    • ++ (Increment)
    • -- (Decrement)
    • ! (Logical NOT)
    • ~ (Bitwise NOT)
  • Postfix and Prefix: Differences in evaluation order.
    • Example: a++ vs ++a

Operator Precedence and Associativity

  • Precedence: Determines the order of evaluation of operators.
    • Higher precedence operators are evaluated first.
    • Example: * has higher precedence than +.
  • Associativity: Determines the order of evaluation when operators have the same precedence.
    • Left to Right: Most operators, except assignment operators.
    • Right to Left: Assignment operators like =.
    • Example: a - b + c (left-to-right) computes as (a - b) + c.*

Summary

  • Understanding of C++ operators including their categories, usage, and evaluation.
  • Hands-on practice by predicting and verifying output.
  • Learned unary, binary, and miscellaneous operators, and their precedence and associativity.

Next Topic

  • C++ Statements