Operators are essential programming tools for carrying out a variety of operations. These special symbols instruct the compiler to perform specific actions on data. Operators are the backbone of any C++ program, from performing calculations to establishing comparisons. But with a diverse set of operators available, understanding their functionalities can feel overwhelming, especially for beginners. Here, we will look at C++ operators and learn how to use them effectively in code.
Operators are symbols or keywords that perform specific operations on one or more operands to produce results. These operations include arithmetic calculations, comparisons, logical operations, assignments, and more. They are the basic building blocks that manipulate data and control the flow of a program.
In C++, operators can be categorized into several types based on functionality and usage. Understanding these types is essential for mastering the language and writing efficient code. Let’s discuss each type in detail and learn about their importance in C++ programming.
Arithmetic operators perform basic mathematical calculations on the operands. These can be further classified into two types:
Unary operators
Unary operators work with a single operand, requiring only one operand or variable to perform their operation. For example:
Check out our Answer on "What is the difference between a++ and ++a?"
Binary operators
Binary operators require two operands or variables to perform their operation. They work with a pair of operands, applying the operation between them. For example:
+
(Addition)
-
(Subtraction)
*
(Multiplication)
/
(Division)
%
(Modulus—calculates the remainder after a division)
These operators allow you to perform calculations on numerical data and store the results in variables. For example:
#include <iostream>int main() {// Declare and initialize variablesint a = 10;int b = 5;// Increment and decrement operatorsstd::cout << "Initial values: a = " << a << ", b = " << b << std::endl;a++;b--;std::cout << "After incrementing a and decrementing b: a = " << a << ", b = " << b << std::endl;// Arithmetic operatorsint addition = a + b;int subtraction = a - b;int multiplication = a * b;int division = a / b;int modulus = a % b;std::cout << "Addition (a + b): " << addition << std::endl;std::cout << "Subtraction (a - b): " << subtraction << std::endl;std::cout << "Multiplication (a * b): " << multiplication << std::endl;std::cout << "Division (a / b): " << division << std::endl;std::cout << "Modulus (a % b): " << modulus << std::endl;return 0;}
Assignment operators are used to assign values to variables. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be the same data type as the variable on the left. Otherwise, the compiler will give an error. These types of operators include:
=
(Assignment)
+=
(Add and assign)
-=
(Subtract and assign)
*=
(Multiply and assign)
/=
(Divide and assign)
%=
(Modulus and assign)
These shorthand operators save you time and make your code more concise. For example:
#include <iostream>int main() {// Declare and initialize variablesint a = 10;int b = 5;std::cout << "a = " << a << " b = " << b << std::endl;// Assignment operatorint c = a;std::cout << "c = " << c << std::endl;// Add and assign operatorc += b; // Equivalent to c = c + b;std::cout << "c after adding b: " << c << std::endl;// Subtract and assign operatorc -= b; // Equivalent to c = c - b;std::cout << "c after subtracting b: " << c << std::endl;// Multiply and assign operatorc *= b; // Equivalent to c = c * b;std::cout << "c after multiplying by b: " << c << std::endl;// Divide and assign operatorc /= b; // Equivalent to c = c / b;std::cout << "c after dividing by b: " << c << std::endl;// Modulus and assign operatorc %= b; // Equivalent to c = c % b;std::cout << "c after taking modulus with b: " << c << std::endl;return 0;}
Relational operators help you compare the values of two operands and determine the relationship between them. These operators come in handy when you need to make decisions based on conditions:
==
(Equal to)
!=
(Not equal to)
<
(Less than)
>
(Greater than)
<=
(Less than or equal to)
>=
(Greater than or equal to)
The result of these comparisons is always a boolean value (true or false), which you can use in conditional statements to control program flow.
#include <iostream>int main() {// Declare and initialize variablesint a = 10;int b = 5;std::cout << "a = " << a << " b = " << b << std::endl;// Equal to operatorif (a == b) {std::cout << "a is equal to b" << std::endl;} else {std::cout << "a is not equal to b" << std::endl;}// Not equal to operatorif (a != b) {std::cout << "a is not equal to b" << std::endl;} else {std::cout << "a is equal to b" << std::endl;}// Less than operatorif (a < b) {std::cout << "a is less than b" << std::endl;} else {std::cout << "a is not less than b" << std::endl;}// Greater than operatorif (a > b) {std::cout << "a is greater than b" << std::endl;} else {std::cout << "a is not greater than b" << std::endl;}// Less than or equal to operatorif (a <= b) {std::cout << "a is less than or equal to b" << std::endl;} else {std::cout << "a is greater than b" << std::endl;}// Greater than or equal to operatorif (a >= b) {std::cout << "a is greater than or equal to b" << std::endl;} else {std::cout << "a is less than b" << std::endl;}return 0;}
Logical operators are used to combine conditional statements to generate more complex logic in our programs. Common logical operators include:
&&
(AND) - Returns true only if both conditions are true.
||
(OR) - Returns true if at least one condition is true.
!
(NOT) - Inverts the truth value of a condition.
Combining these operators allows us to create intricate conditional expressions to handle various scenarios in our code. For example:
#include <iostream>int main() {// Declare and initialize variablesint x = 5;int y = 10;std::cout << "x = " << x << " y = " << y << std::endl;bool condition1 = (x < y);bool condition2 = (x > 0);bool condition3 = (y < 0);std::cout << "Condition 1: " << "Is x < y?" << std::endl;std::cout << "Condition 2: " << "Is x > 0?" << std::endl;std::cout << "Condition 3: " << "Is y < 0?" << std::endl;// AND operator (&&)if (condition1 && condition2) {std::cout << "Both condition1 and condition2 are true." << std::endl;} else {std::cout << "At least one of condition1 and condition2 is false." << std::endl;}// OR operator (||)if (condition1 || condition3) {std::cout << "At least one of condition1 and condition3 is true." << std::endl;} else {std::cout << "Neither condition1 nor condition3 is true." << std::endl;}// NOT operator (!)if (!condition3) {std::cout << "condition3 is false." << std::endl;} else {std::cout << "condition3 is true." << std::endl;}return 0;}
Bitwise operators work on the individual bits of data, enabling low-level manipulation. While not as commonly used as other operators, they can be valuable for specific tasks. Common bitwise operators include:
&
(Bitwise AND) (Returns true only if both bits are true)
|
(Bitwise OR) (Returns true if at least one bit is true)
~
(Bitwise NOT) (Inverts the truth value of a bit)
^
(Bitwise XOR) (Returns true only if the bits in the operands are different)
Only char and int data types can be used with bitwise operators. For example:
#include <iostream>int main() {// Declare and initialize variablesint x = 5; // binary: 0101int y = 10; // binary: 1010std::cout << "x = " << x << " y = " << y << std::endl;// Bitwise AND operator (&)int resultAnd = x & y;std::cout << "Bitwise AND of x and y: " << resultAnd << std::endl;// Bitwise OR operator (|)int resultOr = x | y;std::cout << "Bitwise OR of x and y: " << resultOr << std::endl;// Bitwise NOT operator (~)int resultNotX = ~x;std::cout << "Bitwise NOT of x: " << resultNotX << std::endl;// Bitwise XOR operator (^)int resultXor = x ^ y;std::cout << "Bitwise XOR of x and y: " << resultXor << std::endl;return 0;}
Composing effective code in C++ requires a fundamental understanding of operators. By mastering the various types of operators and their usage, programmers can utilize the full potential of the C++ language. By regularly practicing with different operators, you can be more proficient and creative in C++ programming.
Free Resources