Index
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.
Operator | Name | Description | Example | |
---|---|---|---|---|
+ | Addition | Adds together two values | x + y | |
– | Subtraction | Subtracts one value from another | x – y | |
* | Multiplication | Multiplies two values | x * y | |
/ | Division | Divides one value by another | x / y | |
% | Modulus | Returns the division remainder | x % y | |
++ | Increment | Increases the value of a variable by 1 | ++x | |
— | Decrement | Decreases 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:
Operator | Example | Same As | |
---|---|---|---|
= | x = 5 | x = 5 | |
+= | x += 3 | x = x + 3 | |
-= | x -= 3 | x = x – 3 | |
*= | x *= 3 | x = x * 3 | |
/= | x /= 3 | x = x / 3 | |
%= | x %= 3 | x = x % 3 | |
&= | x &= 3 | x = x & 3 | |
|= | x |= 3 | x = x | 3 | |
^= | x ^= 3 | x = x ^ 3 | |
>>= | x >>= 3 | x = x >> 3 | |
<<= | x <<= 3 | x = 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:
Operator | Name | Example | |
---|---|---|---|
== | Equal to | x == y | |
!= | Not equal | x != y | |
> | Greater than | x > y | |
< | Less than | x < y | |
>= | Greater than or equal to | x >= y | |
<= | Less than or equal to | x <= 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:
- AND Operator (&&): This operator returns true if both of the operands (conditions) are true; otherwise, it returns false.
- 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.
- 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.