Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

ASIAN DEVELOPMENT FOUNDATION COLLEGE

262 P. Burgos St. Tacloban City


Information Technology Department

C++ Operators

Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

Example
int x = 100 + 50;

Although the + operator is often used to add together two values, like in the example
above, it can also be used to add together a variable and a value, or a variable and
another variable:

Example
int sum1 = 100 + 50;        // 150 (100 + 50)
int sum2 = sum1 + 250;      // 400 (150 + 250)
int sum3 = sum2 + sum2;     // 800 (400 + 400)

C++ Arithmetic Operators

Arithmetic Operators in C++ are used to perform arithmetic or mathematical


operations on the operands. For example, ‘+’ is used for addition, ‘–‘ is used for
subtraction,  ‘*’ is used for multiplication, etc. In simple terms, arithmetic operators are
used to perform arithmetic operations on variables and data; they follow the same
relationship between an operator and an operand.

Arithmetic Operator
These operators operate or work with two operands. C++ provides 5 Binary Arithmetic
Operators for performing arithmetic functions:
Name of the
Operator Operation
Operators Implementation

Used in calculating the Addition


Addition x+y
+ of two operands
Name of the
Operator Operation
Operators Implementation

Used in calculating Subtraction


Subtraction x-y
– of two operands

Used in calculating Multiplication


Multiplication x*y
* of two operands

Used in calculating Division of


Division x/y
/ two operands

Used in calculating Remainder


Modulus x%y
% after calculation of two operands

// C++ program to execute all 5


// arithmetic function
#include <iostream>
using namespace std;
  
int main()
{
    int GFG1, GFG2;
    GFG1 = 10;
    GFG2 = 3;
  
    // printing the sum of GFG1 and GFG2
    cout<< "GFG1 + GFG2= " << (GFG1 + GFG2) << endl;
  
    // printing the difference of GFG1 and GFG2
    cout << "GFG1 - GFG2 = " << (GFG1 - GFG2) << endl;
  
    // printing the product of GFG1 and GFG2
    cout << "GFG1 * GFG2 = " << (GFG1 * GFG2) << endl;
  
    // printing the division of GFG1 by GFG2
    cout << "GFG1 / GFG2 = " << (GFG1 / GFG2) << endl;
  
    // printing the modulo of GFG1 by GFG2
    cout << "GFG1 % GFG2 = " << (GFG1 % GFG2) << endl;
  
    return 0;
}

Assignment Operators in C/C++


Assignment operators are used to assigning value to a variable. The left side operand
of the assignment operator is a variable and right side operand of the assignment
operator is a value. The value on the right side must be of the same data-type of the
variable on the left side otherwise the compiler will raise an error.
Different types of assignment operators are shown below:
 “=”: This is the simplest assignment operator. This operator is used to
assign the value on the right to the variable on the left.
For example:
 a = 10;

 b = 20;
 ch = 'y';
 “+=”: This operator is combination of ‘+’ and ‘=’ operators. This operator first
adds the current value of the variable on left to the value on the right and
then assigns the result to the variable on the left.
Example:
 (a += b) can be written as (a = a + b)

If initially value stored in a is 5. Then (a += 6) = 11.


 “-=”This operator is combination of ‘-‘ and ‘=’ operators. This operator first
subtracts the current value of the variable on left from the value on the right
and then assigns the result to the variable on the left.
Example:
 (a -= b) can be written as (a = a - b)

If initially value stored in a is 8. Then (a -= 6) = 2.


 “*=”This operator is combination of ‘*’ and ‘=’ operators. This operator first
multiplies the current value of the variable on left to the value on the right
and then assigns the result to the variable on the left.
Example:
 (a *= b) can be written as (a = a * b)

If initially value stored in a is 5. Then (a *= 6) = 30.


 “/=”This operator is combination of ‘/’ and ‘=’ operators. This operator first
divides the current value of the variable on left by the value on the right and
then assigns the result to the variable on the left.
Example:
 (a /= b) can be written as (a = a / b)
If initially value stored in a is 6. Then (a /= 2) = 3.

Below example illustrates the various Assignment Operators:

// C++ program to demonstrate 


// working of Assignment operators 
  
#include <iostream>
using namespace std;
  
int main() 

  
    // Assigning value 10 to a 
    // 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; 
}
C++ Comparison Operators

A relational operator is used to check the relationship between two


operands. For example,

// checks if a is greater than b


a > b;

Here, > is a relational operator. It checks if a is greater than b or not.


If the relation is true, it returns 1 whereas if the relation is false, it returns 0.
The following table summarizes the relational operators used in C++.

Operator Meaning Example

== Is Equal To 3 == 5

!= Not Equal To 3 != 5

> Greater Than 3 > 5 gives us

< Less Than 3 < 5 gives us

>= Greater Than or Equal To 3 >= 5

<= Less Than or Equal To 3 <= 5

== Operator

The equal to == operator returns


 true - if both the operands are equal or the same
 false - if the operands are unequal
For example,

int x = 10;
int y = 15;
int z = 10;

x == y // false
x == z // true

Note: The relational operator == is not the same as the assignment


operator =. The assignment operator = assigns a value to a variable,
constant, array, or vector. It does not compare two operands.

!= Operator

The not equal to != operator returns


 true - if both operands are unequal
 false - if both operands are equal.
For example,

int x = 10;
int y = 15;
int z = 10;

x != y // true
x != z // false
> Operator

The greater than > operator returns


 true - if the left operand is greater than the right
 false - if the left operand is less than the right
For example,

int x = 10;
int y = 15;

x > y // false
y > x // true

< Operator

The less than operator < returns


 true - if the left operand is less than the right
 false - if the left operand is greater than right
For example,

int x = 10;
int y = 15;
x < y // true
y < x // false

>= Operator

The greater than or equal to >= operator returns


 true - if the left operand is either greater than or equal to the right
 false - if the left operand is less than the right
For example,

int x = 10;
int y = 15;
int z = 10;

x >= y // false
y >= x // true
z >= x // true

<= Operator

The less than or equal to operator <= returns


 true - if the left operand is either less than or equal to the right
 false - if the left operand is greater than right
For example,

int x = 10;
int y = 15;

x > y // false
y > x // true

C++ Logical Operators

We use logical operators to check whether an expression is true or false. If the


expression is true, it returns 1 whereas if the expression is false, it returns 0.
Operator Example Meaning

Logical AND.
&& expression1 && expression 2
true only if all the operands are true.

Logical OR.
|| expression1 || expression 2
true if at least one of the operands is true.

Logical NOT.
! !expression
true only if the operand is false.

C++ Logical AND Operator

The logical AND operator && returns


 true - if and only if all the operands are true.
 false - if one or more operands are false.
Truth Table of && Operator
Let a and b be two operands. 0 represents false while 1 represents true. Then,
a b a && b

0 0 0

0 1 0

1 0 0

1 1 1

As we can see from the truth table above, the && operator returns true only if
both a and b are true.

Note: The Logical AND operator && should not be confused with the Bitwise AND
operator &.

Example 1: C++ OR Operator

// C++ program demonstrating && operator truth table

#include <iostream>
using namespace std;

int main() {
int a = 5;
int b = 9;

// false && false = false


cout << ((a == 0) && (a > b)) << endl;

// false && true = false


cout << ((a == 0) && (a < b)) << endl;

// true && false = false


cout << ((a == 5) && (a > b)) << endl;

// true && true = true


cout << ((a == 5) && (a < b)) << endl;

return 0;
}
Run Code

Output

0
0
0
1

In this program, we declare and initialize two int variables a and b with the


values 5 and 9 respectively. We then print a logical expression

((a == 0) && (a > b))

Here, a == 0 evaluates to false as the value of a is 5. a > b is also false since the
value of a is less than that of b. We then use the AND operator && to combine these
two expressions.
From the truth table of && operator, we know that false && false (i.e. 0 && 0) results in
an evaluation of false (0). This is the result we get in the output.
Similarly, we evaluate three other expressions that fully demonstrate the truth table of
the && operator.
C++ Logical OR Operator

The logical OR operator || returns


 true - if one or more of the operands are true.
 false - if and only if all the operands are false.
Truth Table of || Operator
Let a and b be two operands. Then,
a b a || b

0 0 0

0 1 1

1 0 1

1 1 1

As we can see from the truth table above, the || operator returns false only if
both a and b are false.

Example 2: C++ OR Operator

// C++ program demonstrating || operator truth table

#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = 9;

// false && false = false


cout << ((a == 0) || (a > b)) << endl;

// false && true = true


cout << ((a == 0) || (a < b)) << endl;

// true && false = true


cout << ((a == 5) || (a > b)) << endl;

// true && true = true


cout << ((a == 5) || (a < b)) << endl;

return 0;
}
Run Code

Output

0
1
1
1

In this program, we declare and initialize two int variables a and b with the


values 5 and 9 respectively. We then print a logical expression

((a == 0) || (a > b))

Here, a == 0 evaluates to false as the value of a is 5. a > b is also false since the
value of a is less than that of b. We then use the OR operator || to combine these two
expressions.
From the truth table of || operator, we know that false || false (i.e. 0 || 0) results in an
evaluation of false (0). This is the result we get in the output.
Similarly, we evaluate three other expressions that fully demonstrate the truth table
of || operator.

C++ Logical NOT Operator !

The logical NOT operator ! is a unary operator i.e. it takes only one operand.
It returns true when the operand is false, and false when the operand is true.
Truth Table of the ! Operator
Let a be an operand. Then,

Example 3: C++ ! Operator

// C++ program demonstrating ! operator truth table


#include <iostream>
using namespace std;

int main() {
int a = 5;

// !false = true
cout << !(a == 0) << endl;

// !true = false
cout << !(a == 5) << endl;

return 0;
}
Run Code
Output

1
0

In this program, we declare and initialize an int variable a with the value 5. We then


print a logical expression

!(a == 0)

Here, a == 0 evaluates to false as the value of a is 5. However, we use the NOT
operator ! on a == 0. Since a == 0 evaluates to false, the ! operator inverts the results
of a == 0 and the final result is true.
Similarly, the expression !(a == 5) ultimately returns false because a == 5 is true.

References:

Assignment operators in C/C++. GeeksforGeeks. (2019, October 11). Retrieved from


https://www.geeksforgeeks.org/assignment-operators-in-c-c/

C++ arithmetic operators. GeeksforGeeks. (2022, September 15). Retrieved from


https://www.geeksforgeeks.org/cpp-arithmetic-operators/

C++ assignment operators. (n.d.). Retrieved from


https://www.w3schools.com/cpp/cpp_operators_assignment.asp

C++ relational and logical operators. Programiz. (n.d.). Retrieved from


https://www.programiz.com/cpp-programming/relational-logical-operators

You might also like