Zone Of Makos

Menu icon

Operators in C++

Operators in C++ are symbols that represent specific operations on one or more operands. They allow you to perform various mathematical, logical, and assignment operations in your C++ programs. Understanding and using operators effectively is essential for writing efficient and expressive code. In this lesson, we'll explore the different types of operators available in C++ and how to use them.

Arithmetic Operators

Arithmetic operators are used to perform basic arithmetic operations, such as addition, subtraction, multiplication, division, and modulus. These operators work on numeric operands and produce a result based on the arithmetic operation being performed.


int a = 10;
int b = 5;
int sum = a + b;     // Addition
int difference = a - b;  // Subtraction
int product = a * b;  // Multiplication
int quotient = a / b;  // Division
int remainder = a % b;  // Modulus

Relational Operators

Relational operators are used to compare two values and determine the relationship between them. These operators return a boolean value (true or false) based on the comparison result.


int x = 10;
int y = 5;
bool isGreater = x > y;      // Greater than
bool isLesser = x < y;       // Less than
bool isEqual = x == y;       // Equal to
bool isNotEqual = x != y;    // Not equal to
bool isGreaterOrEqual = x >= y;      // Greater than or equal to
bool isLesserOrEqual = x <= y;       // Less than

Logical Operators

Logical operators are used to perform logical operations on boolean values. These operators are typically used in conditional statements or loops to control the flow of the program.


bool condition1 = true;
bool condition2 = false;
bool resultAnd = condition1 && condition2;  // Logical AND
bool resultOr = condition1 || condition2;   // Logical OR
bool resultNot = !condition1;               // Logical NOT

Assignment Operators

Assignment operators are used to assign values to variables. They can also perform arithmetic operations and assign the result back to the variable.


int x = 10;
x += 5;    // Equivalent to: x = x + 5
x -= 3;    // Equivalent to: x = x - 3
x *= 2;    // Equivalent to: x = x * 2
x /= 4;    // Equivalent to: x = x / 4

Increment and Decrement Operators

Increment and decrement operators are used to increase or decrease the value of a variable by one.


int count = 0;
count++;    // Increment by one (Equivalent to: count = count + 1)
count--;    // Decrement by one (Equivalent to: count = count - 1)

Bitwise Operators

Bitwise operators are used to perform operations on individual bits of numeric operands.


int a = 5;      // Binary: 0101
int b = 3;      // Binary: 0011
int resultAnd = a & b;   // Bitwise AND (Binary: 0001)
int resultOr = a | b;    // Bitwise OR (Binary: 0111)
int resultXor = a ^ b;   // Bitwise XOR (Binary: 0110)
int resultNot = ~a;      // Bitwise NOT (Binary: 1010)

Conclusion

Operators in C++ are powerful tools that enable you to perform a wide range of operations on variables and values. By understanding and utilizing different types of operators, you can write more efficient and expressive code. This lesson provided an overview of arithmetic, relational, logical, assignment, increment/decrement, and bitwise operators in C++. However, there are more operators and advanced concepts to explore. Keep practicing and experimenting with operators to strengthen your C++ programming skills. Let's now dive into control flow concepts.