Operators in C++

 Operators in C++ are symbols that perform operations on variables and values. They are essential for manipulating data and performing calculations within a program. C++ has a rich set of built-in operators that can be categorized as follows:

1. Arithmetic Operators

These operators perform basic mathematical operations:

  • + (Addition): Adds two operands (e.g., x + y)
  • - (Subtraction): Subtracts the second operand from the first (e.g., x - y)
  • * (Multiplication): Multiplies two operands (e.g., x * y)
  • / (Division): Divides the first operand by the second (e.g., x / y)
  • % (Modulus): Returns the remainder of a division (e.g., x % y)
  • ++ (Increment): Increases the value of a variable by 1 (e.g., x++ or ++x)
  • -- (Decrement): Decreases the value of a variable by 1 (e.g., x-- or --x)

2. Assignment Operators

These operators assign values to variables:

  • = (Assignment): Assigns the value on the right to the variable on the left (e.g., x = 10)
  • += (Add and assign): Adds the right operand to the left operand and assigns the result to the left operand (e.g., x += 5 is equivalent to x = x + 5)
  • -= (Subtract and assign): Subtracts the right operand from the left operand and assigns the result to the left operand (e.g., x -= 3 is equivalent to x = x - 3)
  • *= (Multiply and assign): Multiplies the left operand by the right operand and assigns the result to the left operand (e.g., x *= 2 is equivalent to x = x * 2)
  • /= (Divide and assign): Divides the left operand by the right operand and assigns the result to the left operand (e.g., x /= 4 is equivalent to x = x / 4)
  • %= (Modulus and assign): Performs modulus on the left operand by the right operand and assigns the result to the left operand (e.g., x %= 3 is equivalent to x = x % 3)

3. Relational Operators

These operators compare two operands and return a Boolean value (true or false):

  • == (Equal to): Returns true if the operands are equal (e.g., x == y)
  • != (Not equal to): Returns true if the operands are not equal (e.g., x != y)
  • > (Greater than): Returns true if the left operand is greater than the right operand (e.g., x > y)
  • < (Less than): Returns true if the left operand is less than the right operand (e.g., x < y)
  • >= (Greater than or equal to): Returns true if the left operand is greater than or equal to the right operand (e.g., x >= y)
  • <= (Less than or equal to): Returns true if the left operand is less than or equal to the right operand (e.g., x <= y)

4. Logical Operators

These operators combine or modify Boolean expressions:

  • && (Logical AND): Returns true if both operands are true (e.g., x && y)
  • || (Logical OR): Returns true if at least one operand is true (e.g., x || y)
  • ! (Logical NOT): Reverses the logical state of its operand (e.g., !x)

5. Bitwise Operators

These operators perform operations on individual bits of data:

  • & (Bitwise AND): Performs a bitwise AND operation (e.g., x & y)
  • | (Bitwise OR): Performs a bitwise OR operation (e.g., x | y)
  • ^ (Bitwise XOR): Performs a bitwise XOR operation (e.g., x ^ y)
  • ~ (Bitwise NOT): Inverts all the bits of its operand (e.g., ~x)
  • << (Left shift): Shifts the bits of the left operand to the left by the number of positions specified by the right operand (e.g., x << 2)
  • >> (Right shift): Shifts the bits of the left operand to the right by the number of positions specified by the right operand (e.g., x >> 1)

6. Other Operators

C++ also has other operators, including:

  • Ternary or Conditional Operator (? :): A shorthand for if-else statements (e.g., condition ? value_if_true : value_if_false)
  • Comma Operator (,): Evaluates multiple expressions in a single statement (e.g., x = (y = 3, z = y + 1);)
  • Scope Resolution Operator (::): Used to access global variables or members of a class (e.g., MyClass::myVariable)
  • Member Access Operators (. and ->): Used to access members of classes and structs (e.g., object.member or pointer->member)
  • Memory Management Operators (new and delete): Used for dynamic memory allocation and deallocation

This is not an exhaustive list, but it covers the most commonly used operators in C++. Understanding these operators is crucial for writing effective C++ programs.

Comments

Popular posts from this blog

Kotlin Math Operations and Functions Overview

Kotlin Strings: Features and Operations Guide

Kotlin Android Program (QCR) Application Codes That Read Text in Photos