Operators in C++ play a crucial role in performing various operations on data, from basic arithmetic calculations to complex logical manipulations. This article provides an in-depth exploration of the different types of operators in C++, categorised by their functionality, accompanied by illustrative examples.
Arithmetic Operators:
Arithmetic operators are used to perform mathematical calculations on numerical values.
- Addition (+):
cppint sum = 5 + 3; // sum will be 8
- Subtraction (-):
cppint difference = 10 - 4; // difference will be 6
- Multiplication (*):
cppint product = 7 * 2; // product will be 14
- Division (/):
cppdouble quotient = 15.0 / 4; // quotient will be 3.75
- Modulus (%):
cppint remainder = 20 % 3; // remainder will be 2
Relational Operators:
Relational operators compare values and return boolean results.
- Equal to (==):
cppbool isEqual = (5 == 5); // isEqual will be true
- Not equal to (!=):
cppbool isNotEqual = (8 != 5); // isNotEqual will be true
- Greater than (>):
cppbool isGreater = (10 > 7); // isGreater will be true
- Less than (<):
cppbool isLess = (3 < 6); // isLess will be true
- Greater than or equal to (>=):
cppbool isGreaterOrEqual = (12 >= 12); // isGreaterOrEqual will be true
- Less than or equal to (<=):
cppbool isLessOrEqual = (9 <= 7); // isLessOrEqual will be false
Logical Operators:
Logical operators are used to manipulate boolean values.
- Logical AND (&&):
cppbool result = (true && false); // result will be false
- Logical OR (||):
cppbool result = (true || false); // result will be true
- Logical NOT (!):
cppbool result = !(true); // result will be false
Assignment Operators:
Assignment operators are used to assign values to variables.
- Assignment (=):
cppint x = 5;
- Addition assignment (+=):
cppint y = 3;
y += 2; // y will be 5
- Subtraction assignment (-=):
cppint z = 8;
z -= 3; // z will be 5
- Multiplication assignment (*=):
cppint w = 4;
w *= 3; // w will be 12
- Division assignment (/=):
cppint q = 15;
q /= 5; // q will be 3
Bitwise Operators:
Bitwise operators perform operations on individual bits of values.
- Bitwise AND (&):
cppint result = 5 & 3; // result will be 1
- Bitwise OR (|):
cppint result = 5 | 3; // result will be 7
- Bitwise XOR (^):
cppint result = 5 ^ 3; // result will be 6
- Bitwise NOT (~):
cppint result = ~5; // result will depend on the system's representation
- Left shift (<<):
cppint result = 5 << 2; // result will be 20
- Right shift (>>):
cppint result = 15 >> 2; // result will be 3
Conditional (Ternary) Operator:
The conditional operator is a shorthand for an if-else
statement.
cppint max = (a > b) ? a : b; // Assigns the larger value of a and b to max
Increment and Decrement Operators:
Increment (++
) and decrement (--
) operators are used to increase or decrease the value of a variable by 1.
cppint num = 5;
num++; // num will become 6
num--; // num will become 5 again
These operators can be used as a postfix (num++
) or a prefix (++num
) operator, and their behavior differs in certain contexts.
Conditional Statements:
Conditional statements use operators to make decisions and execute specific code blocks.
The if
Statement:
The if
statement is used to execute a block of code if a condition is true.
cppif (condition) {
// Code to execute if the condition is true
}
The else if
and else
Statements:
The else if
statement allows us to check additional conditions if the previous if
or else if
conditions are false. The else
statement is used to execute code if none of the previous conditions are true.
cppif (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if neither condition1 nor condition2 is true
}
Comma Operator:
The comma operator (,
) evaluates multiple expressions sequentially and returns the result of the last expression.
cppint a = 5, b = 3, c;
c = (a++, b++); // a and b are incremented, c holds the value of b (3)
Sizeof Operator:
The sizeof
operator returns the size (in bytes) of a data type or a variable.
cppint sizeInt = sizeof(int); // sizeInt will typically be 4 (bytes)
Pointer and Address-of Operators:
Pointer (*
) and address-of (&
) operators are used with pointers to manipulate memory addresses.
cppint num = 10;
int *ptr = # // ptr holds the address of num
int value = *ptr; // value holds the value stored at the memory location pointed by ptr
Member Access Operators:
Member access operators are used to access class or struct members.
- Dot operator (
.
):
cppstruct Point {
int x;
int y;
};
Point p;
p.x = 3;
- Arrow operator (
->
):
cppPoint *ptr = &p;
ptr->y = 7;
Type Cast Operators:
Type cast operators are used to convert one data type to another.
- C-style cast:
cppint a = 5;
double b = (double)a; // a is cast to double
static_cast
:
cppdouble c = 3.14;
int d = static_cast<int>(c); // c is cast to int
No comments:
Post a Comment