Introduction
Get ready to dive into the heart of C++ programming as we explore the fundamental concept of operators. In this learning journey, we’ll unravel the power of operators and how they play a crucial role in shaping the logic and functionality of your C++ code.
Arithmetic Operators
C++, arithmetic operators allow you to perform basic mathematical operations:- + (Addition):** Adds two values together.
-(Subtraction):** Subtracts the right operand from the left.*(Multiplication):** Multiplies two values./(Division):** Divides the left operand by the right.%(Modulus):** Returns the remainder of the division.
#include
int main() {
int a = 10, b = 3;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
std::cout << "Sum: " << sum << std::endl;
std::cout << "Difference: " << difference << std::endl;
std::cout << "Product: " << product << std::endl;
std::cout << "Quotient: " << quotient << std::endl;
std::cout << "Remainder: " << remainder << std::endl;
return 0;
}
Relational Operators
Relational operators help compare values and produce Boolean results:- == (Equal to): Checks if two values are equal.
!=(Not equal to): Checks if two values are not equal.>(Greater than): Checks if the left value is greater than the right.<(Less than): Checks if the left value is less than the right.>=(Greater than or equal to): Checks if the left value is greater than or equal to the right.<=(Less than or equal to): Checks if the left value is less than or equal to the right.
#include
int main() {
int x = 5, y = 10;
bool isEqual = (x == y);
bool isNotEqual = (x != y);
bool isGreaterThan = (x > y);
bool isLessThan = (x < y);
bool isGreaterOrEqual = (x >= y);
bool isLessOrEqual = (x <= y);
std::cout << "Equal: " << isEqual << std::endl;
std::cout << "Not Equal: " << isNotEqual << std::endl;
std::cout << "Greater Than: " << isGreaterThan << std::endl;
std::cout << "Less Than: " << isLessThan << std::endl;
std::cout << "Greater or Equal: " << isGreaterOrEqual << std::endl;
std::cout << "Less or Equal: " << isLessOrEqual << std::endl;
return 0;
}
Logical Operators
Logical operators perform operations on Boolean values:- && (Logical AND): Returns True if both operands are True.
||(Logical OR): Returns True if at least one operand is True.!(Logical NOT): Returns True if the operand is False and vice versa.
#include
int main() {
bool a = True, b = False;
bool logicalAnd = (a && b);
bool logicalOr = (a || b);
bool logicalNotA = !a;
bool logicalNotB = !b;
std::cout << "Logical AND: " << logicalAnd << std::endl;
std::cout << "Logical OR: " << logicalOr << std::endl;
std::cout << "Logical NOT A: " << logicalNotA << std::endl;
std::cout << "Logical NOT B: " << logicalNotB << std::endl;
return 0;
}
Bitwise Operators
Bitwise operators manipulate individual bits:- & (Bitwise AND): Performs AND operation on each bit.
|(Bitwise OR): Performs OR operation on each bit.^(Bitwise XOR): Performs XOR operation on each bit.~(Bitwise NOT): Flips each bit.
#include
int main() {
int x = 5, y = 3;
int bitwiseAnd = x & y;
int bitwiseOr = x | y;
int bitwiseXor = x ^ y;
int bitwiseNotX = ~x;
std::cout << "Bitwise AND: " << bitwiseAnd << std::endl;
std::cout << "Bitwise OR: " << bitwiseOr << std::endl;
std::cout << "Bitwise XOR: " << bitwiseXor << std::endl;
std::cout << "Bitwise NOT X: " << bitwiseNotX << std::endl;
return 0;
}
Assignment Operators
Assignment operators assign values to variables:- =: Assigns the value on the right to the variable on the left.
+=:Adds the right value to the variable on the left.-=(Subtraction assignment): Subtracts the right value from the variable on the left.*=(Multiplication assignment): Multiplies the variable on the left by the right value./=(Division assignment): Divides the variable on the left by the right value.%=(Modulus assignment): Assigns the remainder of the division of the variable on the left by the right value.
#include
int main() {
int num = 10;
num += 5; // num = num + 5;
std::cout << "After +=: " << num << std::endl;
num -= 3; // num = num - 3;
std::cout << "After -=: " << num << std::endl;
num *= 2; // num = num * 2;
std::cout << "After *=: " << num << std::endl;
num /= 4; // num = num / 4;
std::cout << "After /=: " << num << std::endl;
num %= 3; // num = num % 3;
std::cout << "After %=: " << num << std::endl;
return 0;
}
Exercise: Create and Evaluate Expressions
Now, let’s put your knowledge into action. Create and evaluate expressions using various operators. For example:
#include
int main() {
int a = 7, b = 3, c = 5;
// Create and evaluate expressions
int result1 = a + b * c;
int result2 = (a + b) * c;
bool isTrue = (a > b) && (b < c);
// Display the results
std::cout << "Result 1: " << result1 << std::endl;
std::cout << "Result 2: " << result2 <<
Conclusion
Congratulations on completing the exercise! You’ve successfully applied your knowledge by creating and evaluating expressions with a variety of operators. Keep practicing and exploring, as each expression you craft is a step toward becoming a more proficient coder. Well done!
