Operators
Operators are used to perform operations on variables and values. They can be used to mutate values, perform arithmetic operations, make comparisons, and more.
Arithmetic
Overview
This section covers basic arithmetic operators used in programming for performing mathematical operations.
Addition (+
)
- Adds two operands.
- Example:
a + b
Subtraction (-
)
- Subtracts the second operand from the first.
- Example:
a - b
Multiplication (*
)
- Multiplies two operands.
- Example:
a * b
Division (/
)
- Divides the numerator by the denominator.
- Example:
a / b
Modulus (%
)
- Returns the remainder of a division operation.
- Example:
a % b
Increment (++
)
- Increases the value of an operand by one.
- Example:
a++
or++a
The difference between a++
and ++a
is that a++
returns the value of a
before incrementing, while ++a
returns the value of a
after incrementing.
Example:
int a = 5;
int b = a++; // b is 5, a is 6
int c = ++a; // c is 7, a is 7
Decrement (--
)
- Decreases the value of an operand by one.
- Example:
a--
or--a
Examples
int a = 5;
int b = 3;
int sum = a + b; // 8
int difference = a - b; // 2
int product = a * b; // 15
int quotient = a / b; // 1
int remainder = a % b; // 2
Relational/Comparison
This section explores relational and comparison operators used to compare values and determine their relationships.
Equal to (==
)
- Checks if two operands are equal.
- Example:
a == b
Not equal to (!=
)
- Checks if two operands are not equal.
- Example:
a != b
Greater than (>
)
- Checks if the left operand is greater than the right.
- Example:
a > b
Less than (<
)
- Checks if the left operand is less than the right.
- Example:
a < b
Greater than or equal to (>=
)
- Checks if the left operand is greater than or equal to the right.
- Example:
a >= b
Less than or equal to (<=
)
- Checks if the left operand is less than or equal to the right.
- Example:
a <= b
Examples
int a = 5;
int b = 3;
bool isEqual = (a == b); // false
bool isNotEqual = (a != b); // true
bool isGreater = (a > b); // true
bool isLess = (a < b); // false
bool isGreaterOrEqual = (a >= b); // true
bool isLessOrEqual = (a <= b); // false
Logical Operators
This section covers logical operators used to perform logical operations on boolean values.
Conditional logical AND (&&
)
- Returns true if both operands are true. Does not evaluate the right operand if the left operand is false.
- Example:
a && b
Logical AND (&
)
- Returns true if both operands are true. Always evaluates both operands (unless an exception is thrown).
- Example:
a & b
Conditional logical OR (||
)
- Returns true if at least one of the operands is true. Does not evaluate the right operand if the left operand is true.
- Example:
a || b
Logical OR (|
)
- Returns true if at least one of the operands is true. Always evaluates both operands (unless an exception is thrown).
- Example:
a | b
Logical NOT (!
)
- Inverts the boolean value of the operand.
- Example:
!a
Examples
bool a = true;
bool b = false;
bool andResult = (a && b); // false
bool orResult = (a || b); // true
bool notResult = !a; // false
Assignment Operators
This section explores assignment operators used to assign and modify values of variables.
1. Assignment (=
)
- Assigns a value to a variable.
- Example:
a = b
2. Addition Assignment (+=
)
- Adds a value to a variable and assigns the result.
- Example:
a += b
(equivalent toa = a + b
)
3. Subtraction Assignment (-=
)
- Subtracts a value from a variable and assigns the result.
- Example:
a -= b
(equivalent toa = a - b
)
4. Multiplication Assignment (*=
)
- Multiplies a variable by a value and assigns the result.
- Example:
a *= b
(equivalent toa = a * b
)
5. Division Assignment (/=
)
- Divides a variable by a value and assigns the result.
- Example:
a /= b
(equivalent toa = a / b
)
6. Modulus Assignment (%=
)
- Calculates the modulus of a variable with a value and assigns the result.
- Example:
a %= b
(equivalent toa = a % b
)
Examples
int a = 5;
int b = 3;
a += b; // a is now 8
a -= b; // a is now 5
a *= b; // a is now 15
a /= b; // a is now 5
a %= b; // a is now 2