Operators

Operators are symbols or special characters that perform various operations on operands, such as variables or constants. They are an essential building block of any programming language and are used to manipulate data and perform calculations. In this tutorial, we will cover the different types of operators in C++, including arithmetic, assignment, relational, logical, and bitwise operators.

Arithmetic Operators

Arithmetic operators are used for mathematical calculations and operate on numerical operands. The commonly used arithmetic operators in C++ are:

  • Addition ( + ): Adds two operands.
  • Subtraction ( - ): Subtracts the second operand from the first operand.
  • Multiplication ( * ): Multiplies two operands.
  • Division ( / ): Divides the first operand by the second operand.
  • Modulo ( % ): Returns the remainder after division.
Example:

            #include <iostream>
            using namespace std;
            
            int main() {
                int num1 = 10;
                int num2 = 3;
                int result;
            
                result = num1 + num2;
                cout << "Addition: " << result << endl;
            
                result = num1 - num2;
                cout << "Subtraction: " << result << endl;
            
                result = num1 * num2;
                cout << "Multiplication: " << result << endl;
            
                result = num1 / num2;
                cout << "Division: " << result << endl;
            
                result = num1 % num2;
                cout << "Modulo: " << result << endl;
            
                return 0;
            }
        

Assignment Operators

Assignment operators assign values to variables. The basic assignment operator is '=' and is used to assign the value on the right to the variable on the left. Compound assignment operators combine arithmetic operators with assignment. Some commonly used compound assignment operators are:

  • Addition and assignment (+=)
  • Subtraction and assignment (-=)
  • Multiplication and assignment (*=)
  • Division and assignment (/=)
  • Modulo and assignment (%=)
Example:

            #include <iostream>
            using namespace std;
            
            int main() {
                int num1 = 10;
                int num2 = 3;
            
                num1 += num2;
                cout << "Addition and Assignment: " << num1 << endl;
            
                num1 -= num2;
                cout << "Subtraction and Assignment: " << num1 << endl;
            
                num1 *= num2;
                cout << "Multiplication and Assignment: " << num1 << endl;
            
                num1 /= num2;
                cout << "Division and Assignment: " << num1 << endl;
            
                num1 %= num2;
                cout << "Modulo and Assignment: " << num1 << endl;
            
                return 0;
            }
        

Relational Operators

Relational operators are used to compare the relationship between two operands. They return a boolean value (true or false) based on the comparison. The commonly used relational operators in C++ are:

  • Equal to (==)
  • Not equal to (!=)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)
Example:

            #include <iostream>
            using namespace std;
            
            int main() {
                int num1 = 10;
                int num2 = 3;
                bool result;
            
                result = num1 == num2;
                cout << "Equal to: " << result << endl;
            
                result = num1 != num2;
                cout << "Not equal to: " << result << endl;
            
                result = num1 > num2;
                cout << "Greater than: " << result << endl;
            
                result = num1 < num2;
                cout << "Less than: " << result << endl;
            
                result = num1 >= num2;
                cout << "Greater than or equal to: " << result << endl;
            
                result = num1 <= num2;
                cout << "Less than or equal to: " << result << endl;
            
                return 0;
            }
        

Logical Operators

Logical operators are used to perform logical operations based on boolean values. They return a boolean value as a result. The commonly used logical operators in C++ are:

  • Logical AND (&&)
  • Logical OR (||)
  • Logical NOT (!)
Example:

            #include <iostream>
            using namespace std;
            
            int main() {
                bool isSunny = true;
                bool isWarm = false;
                bool result;
            
                result = isSunny && isWarm;
                cout << "Logical AND: " << result << endl;
            
                result = isSunny || isWarm;
                cout << "Logical OR: " << result << endl;
            
                result = !isSunny;
                cout << "Logical NOT: " << result << endl;
            
                return 0;
            }
        

These are just a few examples of the operators in C++. As you dive deeper into programming, you'll encounter more advanced operators and their functionalities.