0% found this document useful (0 votes)
20 views

Object Oriented Progamming C++ Lecture 4

The document discusses different types of operators in C++ including arithmetic, assignment, relational, logical and bitwise operators. It provides examples of using each operator type and explains their functions.

Uploaded by

thakurrt270904
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Object Oriented Progamming C++ Lecture 4

The document discusses different types of operators in C++ including arithmetic, assignment, relational, logical and bitwise operators. It provides examples of using each operator type and explains their functions.

Uploaded by

thakurrt270904
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Operators are symbols that perform operations on variables and values.

For example, + is an operator used for addition,


 while - is an operator used for subtraction
A + B
A,B (operand)
 + (operator)
OPERATORS IN C++
ARITHMETIC OPERATORS

CAP202| Dr. Ajay Rastogi | Programming Domain


Arithmetic operators are used to perform arithmetic operations on
variables and data.
a + b;
Here, the + operator is used to add two variables a and b
#include <iostream>

using namespace std;

int main() {

int a, b;

a = 7;

b = 2;

cout << "a + b = " << (a + b) << endl;

cout << "a - b = " << (a - b) << endl;

cout << "a * b = " << (a * b) << endl;

cout << "a / b = " << (a / b) << endl;

cout << "a % b = " << (a % b) << endl;

}
OPERATORS IN C++
C++ ASSIGNMENT OPERATORS

CAP202| Dr. Ajay Rastogi | Programming Domain


Assignment operators are used to assign values to variables. For
example.
A=5; // assign 5 to A
OPERATORS IN C++
RELATIONAL OPERATORS

CAP202| Dr. Ajay Rastogi | Programming Domain


A relational operator is used to check the relationship between two
operands
a > b;
 > 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.
#include <iostream> cout << "3 > 5 is " << result << endl;

using namespace std; result = a < b; // true

main() { cout << "3 < 5 is " << result << endl;

int a, b; result = a >= b; // false

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

b = 5; result = a <= b; // true

bool result; 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


OPERATORS IN C++
LOGICAL OPERATORS

CAP202| Dr. Ajay Rastogi | Programming Domain


Logical operators are used 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
logical operators are commonly used in decision making.
Suppose,
a=5
b=8
Then,
(a > 3) && (b > 5) evaluates to true
(a > 3) && (b < 5) evaluates to false
(a > 3) || (b > 5) evaluates to true
(a > 3) || (b < 5) evaluates to true
(a < 3) || (b < 5) evaluates to false
!(a < 3) evaluates to true
#include <iostream> result = (3 != 5) || (3 < 5); // true

using namespace std; cout << "(3 != 5) || (3 < 5) is " << result << endl;

int main() { result = (3 != 5) || (3 > 5); // true

bool result; cout << "(3 != 5) || (3 > 5) is " << result << endl;

result = (3 == 5) || (3 > 5); // false


result = (3 != 5) && (3 < 5); // true
cout << "(3 == 5) || (3 > 5) is " << result << endl;
cout << "(3 != 5) && (3 < 5) is " << result << endl;
result = !(5 == 2); // true
result = (3 == 5) && (3 < 5); // false
cout << "!(5 == 2) is " << result << endl;
cout << "(3 == 5) && (3 < 5) is " << result << endl;
result = !(5 == 5); // false
result = (3 == 5) && (3 > 5); // false
cout << "!(5 == 5) is " << result << endl;
cout << "(3 == 5) && (3 > 5) is " << result << endl;
}
OPERATORS IN C++
BITWISE OPERATORS

CAP202| Dr. Ajay Rastogi | Programming Domain


Bitwise operators perform operations on integer data at the individual
bit-level.
These operations include testing, setting, or shifting the actual bits.
These operators are necessary because the Arithmetic-Logic Unit
(ALU) present in the computer's CPU carries out arithmetic operations
at the bit-level.
BITWISE AND OPERATOR
The bitwise AND & operator returns 1 if and only if both the operands
are 1. Otherwise, it returns 0.
Let a and b be two operands that can only take binary values i.e. 1 and
0.
#include <iostream>
using namespace std;
main()
{
int a = 12, b = 6;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "a & b = " << (a & b) << endl;
BITWISE OR OPERATOR
The bitwise OR | operator returns 1 if at least one of the operands is 1.
Otherwise, it returns 0.
Let a and b be two operands that can only take binary values i.e. 1 or 0.
#include <iostream>
main()
{
int a = 12, b = 25;

cout << "a = " << a << endl;


cout << "b = " << b << endl;
cout << "a | b = " << (a | b) << endl;
BITWISE XOR OPERATOR
The bitwise XOR ^ operator returns 1 if and only if one of the operands
is 1. However, if both the operands are 0, or if both are 1, then the
result is 0.
Let a and b be two operands that can only take binary values i.e. 1 or 0
#include <iostream>
int main()
{
int a = 12, b = 25;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "a ^ b = " << (a ^ b) << endl;
}

You might also like