Operators
Operators are special symbols that perform operations on one or more values (operands). They are the fundamental building blocks for performing calculations, comparisons, and logical operations in programming. This section covers the basic types of operators commonly found in programming languages.
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
Operator | Description | Example |
---|---|---|
+ |
Addition | 5 + 3 (Result: 8) |
- |
Subtraction | 10 - 4 (Result: 6) |
* |
Multiplication | 6 * 7 (Result: 42) |
/ |
Division | 20 / 5 (Result: 4) |
% |
Modulo (Remainder) | 10 % 3 (Result: 1) |
++ |
Increment (adds 1) | x++ (if x was 5, it becomes 6) |
-- |
Decrement (subtracts 1) | y-- (if y was 10, it becomes 9) |
Comparison (Relational) Operators
Comparison operators are used to compare two values. They return a boolean value (true
or false
).
Operator | Description | Example |
---|---|---|
== |
Equal to | 5 == 5 (Result: true ) |
!= |
Not equal to | 5 != 10 (Result: true ) |
> |
Greater than | 10 > 5 (Result: true ) |
< |
Less than | 5 < 10 (Result: true ) |
>= |
Greater than or equal to | 5 >= 5 (Result: true ) |
<= |
Less than or equal to | 10 <= 5 (Result: false ) |
Logical Operators
Logical operators are used to combine multiple conditions or to negate a boolean value.
Operator | Description | Example |
---|---|---|
&& (AND) |
Returns true if both operands are true |
(5 > 3) && (10 == 10) (Result: true ) |
|| (OR) |
Returns true if at least one operand is true |
(5 < 3) || (10 == 10) (Result: true ) |
! (NOT) |
Reverses the boolean value of its operand | !(5 == 5) (Result: false ) |
Assignment Operators
Assignment operators are used to assign values to variables.
Operator | Example | Equivalent To |
---|---|---|
= |
x = 5 |
x = 5 |
+= |
x += 3 |
x = x + 3 |
-= |
x -= 2 |
x = x - 2 |
*= |
x *= 4 |
x = x * 4 |
/= |
x /= 2 |
x = x / 2 |
%= |
x %= 3 |
x = x % 3 |
Bitwise Operators
Bitwise operators operate on the binary representation of numbers.
Note:
Bitwise operators are less common in everyday high-level programming but are crucial for low-level operations, graphics, and performance optimizations. They work directly on the individual bits (0s and 1s) of their operands.
Common bitwise operators include:
&
(Bitwise AND)|
(Bitwise OR)^
(Bitwise XOR)~
(Bitwise NOT)<<
(Left Shift)>>
(Right Shift)
Other Operators
Depending on the programming language, other operators might exist, such as:
- Ternary Operator: A shorthand for an
if-else
statement (e.g.,condition ? value_if_true : value_if_false
). - Type Operators: Used to check the type of a variable or perform type conversions.
- Member Access Operators: Used to access properties or methods of objects (e.g.,
.
,->
).
Example of Operator Precedence:
Operators have a predefined order in which they are evaluated. For instance, multiplication and division are performed before addition and subtraction.
var result = 10 + 5 * 2; // Multiplication (5 * 2 = 10) is done first, then addition (10 + 10)
// result will be 20
Parentheses (()
) can be used to explicitly control the order of evaluation.
var anotherResult = (10 + 5) * 2; // Addition (10 + 5 = 15) is done first, then multiplication (15 * 2)
// anotherResult will be 30
Understanding operators is fundamental to writing effective and efficient code. Familiarize yourself with the operators available in your chosen programming language to leverage their full power.