Hi there, we’re Harisystems
"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".
For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.
1C++ Operators
In C++, operators are symbols or keywords that perform various operations on operands (variables or values). They allow you to manipulate data, perform calculations, make comparisons, and more. Understanding the different types of operators available in C++ is crucial for effective programming. In this article, we will explore some commonly used C++ operators along with examples.
1. Arithmetic Operators
C++ provides several arithmetic operators for performing mathematical calculations. Here are the most commonly used arithmetic operators:
- + Addition: Adds two operands.
- - Subtraction: Subtracts the second operand from the first.
- * Multiplication: Multiplies two operands.
- / Division: Divides the first operand by the second.
- % Modulo: Returns the remainder of the division.
Here's an example that demonstrates the usage of arithmetic operators:
#include <iostream>
int main() {
int num1 = 10;
int num2 = 3;
int sum = num1 + num2;
int difference = num1 - num2;
int product = num1 * num2;
int quotient = num1 / num2;
int remainder = num1 % num2;
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;
}
In the above example, we perform various arithmetic operations on two operands, num1
and num2
, and display the results.
2. Relational Operators
Relational operators are used to compare values and determine the relationship between them. Here are the commonly used relational operators:
- == Equality: Checks if two operands are equal.
- != Inequality: Checks if two operands are not equal.
- > Greater than: Checks if the left operand is greater than the right.
- < Less than: Checks if the left operand is less than the right.
- >= Greater than or equal to: Checks if the left operand is greater than or equal to the right.
- <= Less than or equal to: Checks if the left operand is less than or equal to the right.
Here's an example that demonstrates the usage of relational operators:
#include <iostream>
int main() {
int num1 = 10;
int num2 = 5;
bool isEqual = (num1 == num2);
bool isNotEqual = (num1 != num2);
bool isGreater = (num1 > num2);
bool isLess = (num1 < num2);
bool isGreaterOrEqual = (num1 >= num2);
bool isLessOrEqual = (num1 <= num2);
std::cout << "Equal: " << isEqual << std::endl;
std::cout << "Not Equal: " << isNotEqual << std::endl;
std::cout << "Greater: " << isGreater << std::endl;
std::cout << "Less: " << isLess << std::endl;
std::cout << "Greater or Equal: " << isGreaterOrEqual << std::endl;
std::cout << "Less or Equal: " << isLessOrEqual << std::endl;
return 0;
}
In the above example, we compare the values of num1
and num2
using various relational operators and display the results.
3. Logical Operators
Logical operators are used to combine and manipulate boolean values. Here are the commonly used logical operators:
- && Logical AND: Returns true if both operands are true.
- || Logical OR: Returns true if at least one operand is true.
- ! Logical NOT: Reverses the boolean value of an operand.
Here's an example that demonstrates the usage of logical operators:
#include <iostream>
int main() {
bool isSunny = true;
bool isWarm = false;
bool isBeachDay = isSunny && isWarm;
bool isIndoorDay = !isSunny || !isWarm;
std::cout << "Beach Day: " << isBeachDay << std::endl;
std::cout << "Indoor Day: " << isIndoorDay << std::endl;
return 0;
}
In the above example, we combine the boolean values of isSunny
and isWarm
using logical operators and display the results.
These are just a few examples of the many operators available in C++. Operators provide powerful capabilities to manipulate and evaluate values in your programs. Understanding their usage and behavior is essential for efficient and effective programming.
Experiment with different operators, explore their interactions, and leverage their functionality to solve complex problems in your C++ programs.
4.5L
Learners
20+
Instructors
50+
Courses
6.0L
Course enrollments
Future Trending Courses
When selecting, a course, Here are a few areas that are expected to be in demand in the future:.
Future Learning for all
If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.
View CoursesMost Popular Course topics
These are the most popular course topics among Software Courses for learners