Operators are special symbols or keywords used in programming to perform operations on variables and values. They are fundamental to writing expressions and manipulating data within a program. Operators can be classified into several categories, each serving a different purpose. Here’s an overview of the most common types of operators:
1. Arithmetic Operators
Arithmetic operators perform basic mathematical operations.
- Addition (
+
): Adds two values.
int sum = 5 + 3; // sum is 8
- Subtraction (
-
): Subtracts one value from another.
int difference = 10 - 4; // difference is 6
- Multiplication (
*
): Multiplies two values.
int product = 7 * 6; // product is 42
- Division (
/
): Divides one value by another.
int quotient = 20 / 4; // quotient is 5
- Modulus (
%
): Returns the remainder of a division.
int remainder = 10 % 3; // remainder is 1
2. Relational Operators
Relational operators compare two values and return a Boolean result (true
or false
).
- Equal to (
==
): Checks if two values are equal.
bool isEqual = (5 == 5); // isEqual is true
- Not equal to (
!=
): Checks if two values are not equal.
bool isNotEqual = (5 != 3); // isNotEqual is true
- Greater than (
>
): Checks if one value is greater than another.
bool isGreater = (7 > 5); // isGreater is true
- Less than (
<
): Checks if one value is less than another.
bool isLess = (3 < 5); // isLess is true
- Greater than or equal to (
>=
): Checks if one value is greater than or equal to another.
bool isGreaterOrEqual = (5 >= 5); // isGreaterOrEqual is true
- Less than or equal to (
<=
): Checks if one value is less than or equal to another.
bool isLessOrEqual = (3 <= 5); // isLessOrEqual is true
3. Logical Operators
Logical operators are used to combine multiple Boolean expressions.
- Logical AND (
&&
): Returns true if both expressions are true.
bool result = (5 > 3) && (8 > 6); // result is true
- Logical OR (
||
): Returns true if at least one of the expressions is true.
bool result = (5 > 3) || (8 < 6); // result is true
- Logical NOT (
!
): Returns true if the expression is false.
bool result = !(5 > 3); // result is false
4. Assignment Operators
Assignment operators are used to assign values to variables. They can also perform arithmetic operations as part of the assignment.
- Simple Assignment (
=
): Assigns a value to a variable.
int number = 10;
- Addition Assignment (
+=
): Adds a value to the variable and assigns the result.
int number = 10;
number += 5; // number is now 15
- Subtraction Assignment (
-=
): Subtracts a value from the variable and assigns the result.
int number = 10;
number -= 3; // number is now 7
- Multiplication Assignment (
*=
): Multiplies the variable by a value and assigns the result.
int number = 10;
number *= 2; // number is now 20
- Division Assignment (
/=
): Divides the variable by a value and assigns the result.
int number = 10;
number /= 2; // number is now 5
- Modulus Assignment (
%=
): Computes the modulus of the variable by a value and assigns the result.
int number = 10;
number %= 4; // number is now 2
5. Unary Operators
Unary operators operate on a single operand.
- Increment (
++
): Increases the value of the variable by 1.
int number = 5;
number++; // number is now 6
- Decrement (
--
): Decreases the value of the variable by 1.
int number = 5;
number--; // number is now 4
- Unary Plus (
+
): Indicates a positive value (often optional).
int number = +5; // number is 5
- Unary Minus (
-
): Negates the value.
int number = -5; // number is -5
6. Bitwise Operators
Bitwise operators perform operations on binary representations of integers.
- AND (
&
): Performs a bitwise AND operation.
int result = 5 & 3; // result is 1 (binary 0101 & 0011)
- OR (
|
): Performs a bitwise OR operation.
int result = 5 | 3; // result is 7 (binary 0101 | 0011)
- XOR (
^
): Performs a bitwise XOR operation.
int result = 5 ^ 3; // result is 6 (binary 0101 ^ 0011)
- Complement (
~
): Inverts all the bits.
int result = ~5; // result is -6 (bitwise NOT of 0101)
- Left Shift (
<<
): Shifts bits to the left.
int result = 5 << 1; // result is 10 (binary 0101 << 1)
- Right Shift (
>>
): Shifts bits to the right.
int result = 5 >> 1; // result is 2 (binary 0101 >> 1)
Summary
Operators are essential tools in programming that allow you to perform operations on data, make comparisons, and control the flow of execution. Understanding how to use different operators effectively is crucial for writing functional and efficient code. Each type of operator—arithmetic, relational, logical, assignment, unary, and bitwise—serves a specific purpose and plays a role in manipulating and evaluating data within your programs.
0 Comments