Operators

C++ Operators

Operators are used to perform operations on variables and values.

C++ divides the operators into the following groups:


Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations.

OperatorNameDescriptionExample
+AdditionAdds together two valuesx + y
SubtractionSubtracts one value from anotherx – y
*MultiplicationMultiplies two valuesx * y
/DivisionDivides one value by anotherx / y
%ModulusReturns the division remainderx % y
++IncrementIncreases the value of a variable by 1++x
DecrementDecreases the value of a variable by 1–x

Assignment Operators

Assignment operators are used to assign values to variables.

A list of all assignment operators:

OperatorExampleSame As
=x = 5x = 5
+=x += 3x = x + 3
-=x -= 3x = x – 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
&=x &= 3x = x & 3
|=x |= 3x = x | 3
^=x ^= 3x = x ^ 3
>>=x >>= 3x = x >> 3
<<=x <<= 3x = x << 3

Comparison Operators

Comparison operators are essential tools in programming that allow us to evaluate relationships between values or variables. By using these operators, programmers can determine whether certain conditions are true or false. The result of a comparison operation yields a Boolean value, which signifies either true or false. In simpler terms, Boolean values can be thought of as affirmations or negations of conditions. When a comparison is true, it returns 1, indicating affirmation, while false comparisons return 0, representing negation. Therefore, Boolean values serve as indicators of the validity of conditions within a program, aiding in decision-making processes and logic flow.

A list of all comparison operators:

OperatorNameExample
==Equal tox == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

Logical operators

Logical operators are fundamental components of programming languages used to manipulate Boolean values. These operators allow programmers to combine multiple conditions or Boolean expressions to form more complex logical statements. There are typically three main logical operators:

  1. AND Operator (&&): This operator returns true if both of the operands (conditions) are true; otherwise, it returns false.
  2. OR Operator (||): This operator returns true if at least one of the operands (conditions) is true; it returns false only if both operands are false.
  3. NOT Operator (!): This operator is a unary operator that negates the Boolean value of its operand. If the operand is true, the NOT operator returns false, and vice versa.

Logical operators are essential for creating conditional statements, loops, and other decision-making structures in programming. They allow developers to construct complex logical expressions and control the flow of a program based on various conditions.

    Leave a Reply

    Your email address will not be published.

    Need Help?