Chapter 5
Chapter 5
Introduction to Programming
Chapter 5
Operators
Introduction to Programming
Learning outcomes:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Increment / decrement Operators
Ternary Operators
Introduction to Programming
Operator
• An operator is a symbol that is used to perform operations.
• Different types of operators perform different types of operations in C+
+ language.
Introduction to Programming
Types of Operator
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operator
6. Increment / decrement Operators
7. Ternary or Conditional Operator
Introduction to Programming
Introduction to Programming
1. Arithmetic Operators
• Arithmetic operators are used to perform common mathematical operations.
2. Relational Operator
A relational operator is used to check the relationship between two operands.
if the values of two operands are equal, then condition becomes true else its value become
false.
Operator Meaning Example
3. Logical Operator
• Logical operators are used to determine the logic between variables or
values.
• 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.
Introduction to Programming
3. Logical Operator
3. Logical Operator
The logical operators && and || are used when evaluating two expressions to
obtain a single relational result.
4. Bitwise Operator
In C++, bitwise operators are used to perform operations on individual bits.
Operator Description
| Binary OR
^ Binary XOR
4. Bitwise Operator
p q p&q p|q ~p p^q
0 0 0 0 1 0
0 1 0 1 1 1
1 0 0 1 0 1
1 1 1 1 0 0
Introduction to Programming
4. Bitwise Operator
There are two shift operators in C++ programming:
1. Right shift operator >>
2. Left shift operator <<
Introduction to Programming
Example
#include <iostream>
using namespace std;
int main() {
int a = 12, b = 25;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "a & b = " << (a & b) << endl;
return 0;
Introduction to Programming
5. Assignment Operator
In C++, assignment operators are used to assign values to variables.
Example:
int x = 10;
Introduction to Programming
5. Assignment Operator
Operator Example Same As
= x=5 x=5
+= x+=3 x=x+3
-= x-=3 x=x-3
*= x*=3 x=x*3
/= x/=3 x=x/3
%= x%=3 x=x%3
Introduction to Programming
Example:
#include <iostream>
using namespace std;
int main() {
int x = 5;
x += 3;
cout << x;
return 0;
}
Introduction to Programming
Example:
#include <iostream>
#include <string>
using namespace std;
int main() {
int time = 20;
string result = (time < 18) ? "Good day." : "Good evening.";
cout << result;
return 0;
}