Operators in C++
Operators in C++
Introduction
Programmers may create effective and sophisticated programs using the robust programming language C++, which offers several tools and capabilities. Programming in C++ relies on operators, one of its basic building blocks. The idea of operators in C++ will be discussed in this tutorial, along with how to use them to modify data, carry out computations, and direct the course of a program.
What are Operators?
Operators are symbols or keywords used in programming that carry out particular operations on one or more operands (variables or constants) to produce a result. They provide programmers shortcuts for frequent processes, enabling them to create clear and expressive code. Various operators are available in C++ and are divided into different groups according to their performance.
1.Arithmetic Operators
Arithmetic Operators that perform fundamental operations like Addition, Subtraction, Division, Multiplication, and Modulo operations.
The table below will show the operators available in the arithmetic operators:
Operator | Description | Example |
+ | Addition operation | a+b; |
- | Subtraction operation | a-b; |
* | Multiplication operation | a*b; |
/ | Division operation | a/b; |
% | Modular Division | a%b; |
Example Program
#include <iostream>
int main() {
// Addition
int sum = 5 + 3;
std::cout << "Sum: " << sum << std::endl;
// Subtraction
int difference = 8 - 3;
std::cout << "Difference: " << difference << std::endl;
// Multiplication
int product = 5 * 3;
std::cout << "Product: " << product << std::endl;
// Division
float result = 10.0 / 3;
std::cout << "Result: " << result << std::endl;
// Modulo
int remainder = 10 % 3;
std::cout << "Remainder: " << remainder << std::endl;
return 0;
}
Output
Sum: 8
Difference: 5
Product: 15
Result: 3.33333
Remainder: 1
![Operators in C++](https://static.tutorialandexample.com/cpp/operators-in-cpp1.png)
2.Relational Operators
To compare two values and assess their connection, relational operators are utilized. A Boolean value ('true or false') is returned based on the comparison outcome. The relational operators in C++ are listed in the following table:
Operator | Description | Example |
== | Equals to | a==b; |
!= | Not Equals to | a!=b; |
< | Less than | a<b; |
> | Greater than | a>b; |
<= | Less than or Equal to | a<=b; |
>= | Greater than or Equal to | a>=b; |
Example Program
#include<iostream>
int main(){
// Greater than
int a =5, b =3;
bool greaterThan =(a > b);
std::cout << "a > b: " << std::boolalpha << greaterThan << std::endl;
// Less than or equal to
int c = 7, d = 7;
bool lessThanOrEqual = (c <= d);
std::cout << "c <= d: " << std::boolalpha << lessThanOrEqual << std::endl;
// Equal to
int e = 10, f = 10;
bool equalTo = (e == f);
std::cout << "e == f: " << std::boolalpha << equalTo << std::endl;
// Not equal to
int g = 15, h = 20;
bool notEqualTo = (g != h);
std::cout << "g != h: " << std::boolalpha << notEqualTo << std::endl;
// Greater than or equal to
int i =12, j =9;
bool greaterThanOrEqual = (i >= j);
std::cout << "i >= j: " << std::boolalpha << greaterThanOrEqual << std::endl;
// Less than
int k = 4, l = 6;
bool lessThan = (k < l);
std::cout << "k < l: " << std::boolalpha << lessThan << std::endl;
return 0;
}
Output
a > b: true
c <= d: true
e == f: true
g != h: true
i >= j: true
k < l: true
![Operators in C++](https://static.tutorialandexample.com/cpp/operators-in-cpp2.png)
3.Logical Operators
Logical operators are needed to combine numerous conditions and carry out logical operations. They are frequently used in conditional statements and loops to regulate the program's flow. The logical operators in C++ are shown in the table below:
Operator | Description | Example |
&& | Logical AND | (a&&b); |
|| | Logical OR | (a||b); |
! | Logical NOT | ! a; |
Example Program
#include<iostream>
int main(){
// Logical AND
bool condition1 = true;
bool condition2 = false;
bool logicalAnd = (condition1 && condition2);
std::cout << "condition1 && condition2: " << std::boolalpha << logicalAnd << std::endl;
// Logical OR
bool condition3 = true;
bool condition4 = false;
bool logicalOr = (condition3 || condition4);
std::cout << "condition3 || condition4: " << std::boolalpha << logicalOr << std::endl;
// Logical NOT
bool condition5 = true;
bool logicalNot = !condition5;
std::cout << "!condition5: " << std::boolalpha << logicalNot << std::endl;
return 0;
}
Output
condition1 && condition2: false
condition3 || condition4: true
!condition5: false
![Operators in C++](https://static.tutorialandexample.com/cpp/operators-in-cpp3.png)
4.Assignment Operators
To assign values to variables, assignment operators are employed. They pair the assignment operation with another. The most common assignment operator is the equals sign (=), which assigns the value on the right side to the variable on the left side. C++ additionally has compound assignment operators like '+=, -=, *=, /=' that operate and then assign the result to a variable.
Example Program
#include<iostream>
int main(){
// Assignment operator
int value = 5;
std::cout << "Initial value:" << value << std::endl;
// Compound assignment operators
value += 3;
std::cout << "After += 3:" << value << std::endl;
value -= 2;
std::cout << "After -= 2:" << value << std::endl;
value *= 4;
std::cout << "After *= 4:" << value << std::endl;
value /= 2;
std::cout << "After /= 2:" << value << std::endl;
value %= 3;
std::cout << "After %= 3:" << value << std::endl;
return 0;
}
Output
Initial value: 5
After += 3: 8
After -= 2: 6
After *= 4: 24
After /= 2: 12
After %= 3: 0
![Operators in C++](https://static.tutorialandexample.com/cpp/operators-in-cpp4.png)
5.Increment and Decrement Operator
The Increment ('++') and decrement ('--') operators are used to increase or decrease the value of a variable by 1. They are prefix and postfix operators. When used as a prefix operator ('++x or --x'), the value is incremented or decremented before to evaluating the expression. The value is incremented or decremented after the expression is evaluated when used as a postfix operator ('x++ or x--').
Example Program
#include<iostream>
int main(){
int value = 5;
// Increment operator
value++;
std::cout << "After increment:" << value << std::endl;
// Decrement operator
value--;
std::cout << "After decrement:" << value << std::endl;
// Prefix increment operator
++value;
std::cout << "After prefix increment:" << value << std::endl;
// Prefix decrement operator
--value;
std::cout << "After prefix decrement:" << value << std::endl;
return 0;
}
Output
After increment: 6
After decrement: 5
After prefix increment: 6
After prefix decrement: 5
![Operators in C++](https://static.tutorialandexample.com/cpp/operators-in-cpp5.png)
6.Bitwise Operator
Individual bits of integral data types are manipulated using bitwise operators. They operate at the bit level, allowing programmers to interact with binary data representations. The bitwise operators available in C++ are summarized in the table below:
Operator | Description |
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise Complement |
<< | Bitwise Left Shift |
>> | Bitwise Right Shift |
Example Program
#include<iostream>
int main(){
// Bitwise AND
int a = 5; // binary: 0101
int b = 3; // binary: 0011
int bitwiseAnd = a & b; // binary: 0001
std::cout << "Bitwise AND:" << bitwiseAnd << std::endl;
// Bitwise OR
int bitwiseOr = a | b; // binary: 0111
std::cout << "Bitwise OR:" << bitwiseOr << std::endl;
// Bitwise XOR
int bitwiseXor = a ^ b; // binary: 0110
std::cout << "Bitwise XOR:" << bitwiseXor << std::endl;
// Bitwise NOT
int c = 5; // binary: 0101
int bitwiseNot = ~c; // binary: 1010
std::cout << "Bitwise NOT:" << bitwiseNot << std::endl;
// Left shift
int d = 5; // binary: 0101
int leftShift = d << 2; // binary: 010100 (decimal: 20)
std::cout << "Left Shift:" << leftShift << std::endl;
// Right shift
int e = 20; // binary: 010100
int rightShift = e >> 2; // binary: 000101 (decimal: 5)
std::cout << "Right Shift:" << rightShift << std::endl;
return 0;
}
Output
Bitwise AND: 1
Bitwise OR: 7
Bitwise XOR: 6
Bitwise NOT: -6
Left Shift: 20
Right Shift: 5
![Operators in C++](https://static.tutorialandexample.com/cpp/operators-in-cpp6.png)
7.Other Operators
Other operators provided by C++ include the ternary operator (?:), which is shorthand for an 'if-else' statement, and the scope resolution operator (::), which is used to access variables and functions in multiple contexts.
Conclusion:
Operators are very crucial components of C++ programming language because they allow developers to execute a broad range of operations on variables and Constants. Understanding these operators and their capabilities is essential for building effective and concise C++ code.