Arithmetic, Relational, and Logical Operators in C++
C++ provides a wide range of operators for performing operations on variables and values. These operators are categorized into arithmetic, relational, and logical operators. In this article, we will explore these categories in detail with examples.
1. Arithmetic Operators in C++
Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus.
List of Arithmetic Operators
+
: Addition-
: Subtraction*
: Multiplication/
: Division%
: Modulus (remainder of division)
Example of Arithmetic Operators
#include <iostream> int main() { int a = 10, b = 5; std::cout << "Addition: " << (a + b) << std::endl; std::cout << "Subtraction: " << (a - b) << std::endl; std::cout << "Multiplication: " << (a * b) << std::endl; std::cout << "Division: " << (a / b) << std::endl; std::cout << "Modulus: " << (a % b) << std::endl; return 0; }
In this example, we have two variables a
and b
. The program demonstrates the use of all the arithmetic operators to perform various operations and display the results.
2. Relational Operators in C++
Relational operators are used to compare two values or expressions. These operators return a boolean value, either true
or false
, based on the result of the comparison.
List of Relational Operators
==
: Equal to!=
: Not equal to>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal to
Example of Relational Operators
#include <iostream> int main() { int a = 10, b = 5; std::cout << "Is a equal to b? " << (a == b) << std::endl; std::cout << "Is a not equal to b? " << (a != b) << std::endl; std::cout << "Is a greater than b? " << (a > b) << std::endl; std::cout << "Is a less than b? " << (a < b) << std::endl; std::cout << "Is a greater than or equal to b? " << (a >= b) << std::endl; std::cout << "Is a less than or equal to b? " << (a <= b) << std::endl; return 0; }
This example demonstrates how relational operators are used to compare two variables a
and b
. Each comparison will return either true
or false
.
3. Logical Operators in C++
Logical operators are used to perform logical operations on boolean values or expressions. These operators are often used in decision-making and control flow structures like if
and while
.
List of Logical Operators
&&
: Logical AND||
: Logical OR!
: Logical NOT
Example of Logical Operators
#include <iostream> int main() { bool x = true, y = false; std::cout << "x AND y: " << (x && y) << std::endl; std::cout << "x OR y: " << (x || y) << std::endl; std::cout << "NOT x: " << (!x) << std::endl; return 0; }
In this example, we use logical operators to perform logical operations on the boolean variables x
and y
. The program demonstrates how the AND
, OR
, and NOT
operators work in C++.
4. Combining Operators
You can combine arithmetic, relational, and logical operators to form more complex expressions. Here’s an example where we combine different types of operators.
Example of Combining Operators
#include <iostream> int main() { int a = 10, b = 5, c = 15; // Combine arithmetic, relational, and logical operators if ((a + b > c) && (a != b)) { std::cout << "The condition is true!" << std::endl; } else { std::cout << "The condition is false!" << std::endl; } return 0; }
This example demonstrates how to combine operators to evaluate a condition. The condition checks if a + b
is greater than c
and if a
is not equal to b
using a logical AND operation.
5. Summary of Operators
Operator | Category | Description | Example |
---|---|---|---|
+ | Arithmetic | Adds two values. | a + b |
- | Arithmetic | Subtracts two values. | a - b |
* | Arithmetic | Multiplies two values. | a * b |
/ | Arithmetic | Divides two values. | a / b |
% | Arithmetic | Returns the remainder of division. | a % b |
== | Relational | Checks if two values are equal. | a == b |
!= | Relational | Checks if two values are not equal. | a != b |
> | Relational | Checks if the left value is greater than the right. | a > b |
< | Relational | Checks if the left value is less than the right. | a < b |
>= | Relational | Checks if the left value is greater than or equal to the right. | a >= b |
<= | Relational | Checks if the left value is less than or equal to the right. | a <= b |
&& | Logical | Logical AND operation. | x && y |
|| | Logical | Logical OR operation. | x || y |
! | Logical | Logical NOT operation. | !x |
Conclusion
In C++, operators play a crucial role in performing various operations. Arithmetic operators are used for mathematical computations, relational operators help in comparing values, and logical operators are essential for decision-making and control flow. Understanding these operators is fundamental to writing effective C++ programs.