C++ Operators

C++ Operators

  • Operator in C++ is a symbol or keyword used to perform operations on operands.
  • Operands can be variables, constants, or expressions, and operators dictate how these operands are combined.

Operators in C++

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Unary Operators
  • Ternary or Conditional Operators
  • Miscellaneous Operators

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations. Here are some commonly used arithmetic operators in C++:
  • Addition (+): Adds two values together.
  • Subtraction (-): Subtracts one value from another.
  • Multiplication (*): Multiplies two values.
  • Division (/): Divides one value by another.
  • Modulus (%): Returns the remainder of a division operation.
Loading…
output:
Loading…

Relational Operators

  • Relational operators are used to comparing values and determine the relationship between them.
  • These operators return either true or false. Common relational operators include:
  • Equal to (==): Checks if two values are equal.
  • Not equal to (!=): Checks if two values are not equal.
  • Greater than (>): Checks if one value is greater than another.
  • Less than (<): Check if one value is less than another.
  • Greater than or equal to (>=): Checks if one value is greater than or equal to another.
  • Less than or equal to (<=): Checks if one value is less than or equal to another.
Loading…
output is :
Loading…

Logical Operators

  • Logical operators are used to perform logical operations on Boolean values.
  • They are often used in conditional statements and loops. The common logical operators are:
  • Logical AND (&&): Returns true if both operands are true.
  • Logical OR (||): Returns true if at least one operand is true.
  • Logical NOT (!): Returns the opposite of the operand's value.
Loading…
Logical AND (&&):
  • Operand 1: a (with a value of 10)
  • Operand 2: b (with a value of 0)
  • Result: The logical AND operator (&&) evaluates to true (1) if both operands are true (non-zero). In this case, a && b evaluates to 0 (false) because b is 0, which is considered false.
Logical OR (||):
  • Operand 1: a (with a value of 10)
  • Operand 2: b (with a value of 0)
  • Result: The logical OR operator (||) evaluates to true (1) if at least one operand is true (non-zero). In this case, a || b evaluates to 1 (true) because a is 10, which is considered true.
Logical NOT (!):
  • Operand A: a (with a value of 10)
  • Operand B: b (with a value of 0)
  • Result (a): The logical NOT operator (!) negates the value of the operand. In this case, !a evaluates to 0 (false) because a is initially true (non-zero), and the NOT operator flips it to false (0).
  • Result (b): Similarly, !b evaluates to 1 (true) because b is initially false (0), and the NOT operator flips it to true (1).
So, in summary:
  • a && b evaluates to 0 (false) because both operands are not true.
  • a || b evaluates to 1 (true) because at least one operand is true.
  • !a flips the value of a from true to false.
  • !b flips the value of b from false to true.
output is:
Loading…

Bitwise Operators

  • Bitwise operators work at the bit level and are used to manipulate individual bits within data.
  • They are especially useful when dealing with hardware or low-level operations. Common bitwise operators include:
  • Bitwise AND (&): Performs a bitwise AND operation.
  • Bitwise OR (|): Performs a bitwise OR operation.
  • Bitwise XOR (^): Performs a bitwise XOR (exclusive OR) operation.
Loading…
output is :
Loading…

Assignment Operators

The assignment operator (=) is used to assign a value to a variable. It is one of the most basic operators in C++.
Loading…

Unary Operators

  • Unary operators operate on a single operand.
  • An example is the unary minus (-) operator, which negates a value.
Loading…

Ternary or Conditional Operators

  • The ternary operator (? :) is a concise way to write conditional statements.
  • It evaluates a condition and returns one of two values based on whether the condition is true or false.
Loading…
output is :
Loading…

Miscellaneous Operators

  • There are several other operators in C++, such as the address-of operator (&) and the pointer dereference operator (*), which are often used in more advanced programming scenarios.
  • However, for beginners, understanding the operators mentioned above is a great starting point.