C++ 03: Operators in C++

 Operators in C++


1) Arithmetic Operators in C++

  • Addition Operator  + 
  • Subtraction Operator -
  • Multiplication Operator *
  • Division Operator /
  • Modulo Operator %
Program in C++ to demonstrate Arithmetic Operators

#include <iostream>
using namespace std;

int main() {
    int a, b;
    a = 7;
    b = 2;

    // printing the sum of a and b
    cout << "a + b = " << (a + b) << endl;

    // printing the difference of a and b
    cout << "a - b = " << (a - b) << endl;

    // printing the product of a and b
    cout << "a * b = " << (a * b) << endl;

    // printing the division of a by b
    cout << "a / b = " << (a / b) << endl;

    // printing the modulo of a by b
    cout << "a % b = " << (a % b) << endl;

    return 0;
}

Increment and Decrement Operators

C++ also provides increment and decrement operators: ++ and -- respectively. ++ increases the value of the operand by 1, while -- decreases it by 1.

For example,

int num = 5;

// increasing num by 1
++num;

Here, the value of num gets increased to 6 from its initial value of 5.

Example 2: Increment and Decrement Operators

// Working of increment and decrement operators

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 100, result_a, result_b;

    // incrementing a by 1 and storing the result in result_a
    result_a = ++a;
    cout << "result_a = " << result_a << endl;


    // decrementing b by 1 and storing the result in result_b   
    result_b = --b;
    cout << "result_b = " << result_b << endl;

    return 0;
}

Output

result_a = 11
result_b = 99

In the above program, we used ++ and -- operator as prefixes. We can also use these operators as postfix.

There is a slight difference when these operators are used as a prefix versus when they are used as a postfix.


2) Assignment Operators:

  • Simple Assignment  =
  • Add Assign  +=
  • Subtract Assign   -=
  • Multiply Assign  *=
  • Divide Assign    /=

In C++, assignment operators are used to assign values to variables. For example,

// assign 5 to a
a = 5;

Program to demonstrate Assignment Operators: 


#include <iostream> 
using namespace std; 
  
int main()  
{  
  
    // Assigning value 10 using "=" operator  
    int a = 10;  
    cout << "Value of a is "<<a<<"\n";  
  
    // Assigning value by adding 10 to a  
    // using "+=" operator  
    a += 10;  
    cout << "Value of a is "<<a<<"\n";  
  
    // Assigning value by subtracting 10 from a  
    // using "-=" operator  
    a -= 10;  
    cout << "Value of a is "<<a<<"\n";  
  
    // Assigning value by multiplying 10 to a  
    // using "*=" operator  
    a *= 10;  
    cout << "Value of a is "<<a<<"\n";  
  
    // Assigning value by dividing 10 from a  
    // using "/=" operator  
    a /= 10;  
    cout << "Value of a is "<<a<<"\n";  
  
    return 0;  

OperatorExampleEquivalent to
=a = b;a = b;
+=a += b;a = a + b;
-=a -= b;a = a - b;
*=a *= b;a = a * b;
/=a /= b;a = a / b;
%=a %= b;a = a % b;


3) Comparison Operator
  • Equals to ==
  • Less Than <
  • Less than equals <=
  • Greater Than >
  • Greater than equals >=
  • Not Equals to !=
Program to demonstrate Comparison Operators

#include <iostream>
using namespace std;

int main() {
    int a, b;
    a = 3;
    b = 5;
    bool result;

    result = (a == b);   // false
    cout << "3 == 5 is " << result << endl;

    result = (a != b);  // true
    cout << "3 != 5 is " << result << endl;

    result = a > b;   // false
    cout << "3 > 5 is " << result << endl;

    result = a < b;   // true
    cout << "3 < 5 is " << result << endl;

    result = a >= b;  // false
    cout << "3 >= 5 is " << result << endl;

    result = a <= b;  // true
    cout << "3 <= 5 is " << result << endl;

    return 0;
}

4)Logical Operators
  • Logical AND &&
  • Logical OR ||
  • Logical NOT !

Operator

Example

Meaning

&&

expression1 && expression2

Logical AND.
True only if all the operands are true.

||

expression1 || expression2

Logical OR.
True if at least one of the operands is true.

!

!expression

Logical NOT.
True only if the operand is false.


Program to demonstrate Logical Operators:
#include <iostream>
using namespace std;
int main()
{
    //Logical AND
    int result = 0 && 1;
    cout << "Result: 0 && 1 :" << result << endl;
    //Logical OR
    result = 0 || 1;
    cout << "Result: 0 || 1 :" << result << endl;
    //Logical Not
    result = !1;
    cout << "Result: ! 1    :" << result << endl;
    return 0;
}







Comments