0% found this document useful (0 votes)
1 views19 pages

Pfa 6

The document provides an overview of various types of operators in programming, including arithmetic, assignment, relational, logical, bitwise, and unary operators. It includes examples of each type of operator and explains their usage in C++. Additionally, it covers operator precedence, constants, and type conversion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views19 pages

Pfa 6

The document provides an overview of various types of operators in programming, including arithmetic, assignment, relational, logical, bitwise, and unary operators. It includes examples of each type of operator and explains their usage in C++. Additionally, it covers operator precedence, constants, and type conversion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

CS-2210 Programming Fundamentals

Operators

Arbish Akram

Department of Computer Science


Government College University

Arbish Akram 1 / 19
Operators

An operator is a symbol that tells the compiler to perform specific


mathematical or logical manipulations.
They are classified into several categories:
Arithmetic operators (+, −, ∗, /, %)
Assignment operators (=, + =, − =, ∗ =, / =, % =)
Relational operators (==, ! =, >, <, >=, <=)
Logical operators (&&, ||, !)
Bitwise operators (&, |, ∧, ∼, <<, >>)
Unary operators (++, −−, +, −)

Arbish Akram 2 / 19
Arithmetic Operators

Operator Meaning Example


+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus (Remainder) x%y

Arbish Akram 3 / 19
Example 1: Test arithmetic operations
1 #include <iostream>
2 using namespace std;
3 int main()
4 {
5 double num1, num2;
6 double sum, difference, product, quotient;
7
8 cout << "Enter two integers (separated by space): ";
9 cin >> num1 >> num2;
10
11 sum = num1 + num2;
12 difference = num1 - num2;
13 product = num1 * num2;
14 quotient = num1 / num2;
15
16 cout << "Sum: " << sum << endl;
17 cout << "Difference: " << difference << endl;
18 cout << "Product: " << product << endl;
19 cout << "Quotient: " << quotient << endl;
20 return 0;
21 }

Arbish Akram 4 / 19
Assignment Operators

Operator Example
= a = b;
+= a += b;
−= a -= b;
∗= a *= b;
/= a /= b;
%= a %= b;

Arbish Akram 5 / 19
Assignment Operators
1 #include <iostream>
2 using namespace std;
3
4 int main() {
5 int a = 10;
6 int b = 5;
7
8 a = b;
9 cout << "a after a = b: " << a << endl;
10 a += b;
11 cout << "a after a += b: " << a << endl;
12 a -= b;
13 cout << "a after a -= b: " << a << endl;
14 a *= b;
15 cout << "a after a *= b: " << a << endl;
16 a /= b;
17 cout << "a after a /= b: " << a << endl;
18 a %= b;
19 cout << "a after a %= b: " << a << endl;
20 return 0;
21 }

Arbish Akram 6 / 19
Relational Operators

Operator Meaning Example


== Equal to: Returns true if the operands are 5 == 5
equal.
!= Not equal to: Returns true if the 10 != 5
operands are not equal.
> Greater than: Returns true if the left 10 > 5
operand is greater than the right operand.
< Less than: Returns true if the left operand 5 < 10
is less than the right operand.
>= Greater than or equal to: Returns true if 5 >= 5
the left operand is greater than or equal
to the right operand.
<= Less than or equal to: Returns true if the 5 <= 10
left operand is less than or equal to the
right operand.

Arbish Akram 7 / 19
Relational Operators

1 #include <iostream>
2 using namespace std;
3
4 int main() {
5 cout << "Testing Relational Operators:" << endl;
6 cout << "5 == 5: " << (5 == 5) << endl;
7 cout << "10 != 5: " << (10 != 5) << endl;
8 cout << "10 > 5: " << (10 > 5) << endl;
9 cout << "5 < 10: " << (5 < 10) << endl;
10 cout << "5 >= 5: " << (5 >= 5) << endl;
11 cout << "5 <= 10: " << (5 <= 10) << endl;
12
13 return 0;
14 }

Arbish Akram 8 / 19
Logical Operators

Logical operators are used to perform logical operations on boolean


values.
They are typically used to combine multiple conditions in
decision-making statements.

Operator Meaning Example


&& Logical AND if (x > 0 && x < 10)
|| Logical OR if (x == 0 || y == 0)
! Logical NOT if (!isFinished)

Arbish Akram 9 / 19
Logical Operators

1 #include <iostream>
2 using namespace std;
3 int main() {
4 int x = 5;
5 int y = 10;
6 bool isFinished = false;
7
8 // Logical AND (&&)
9 if (x > 0 && x < 10)
10 {
11 cout << "x is between 0 and 10" << endl;
12 }
13 else
14 {
15 cout << "x is not between 0 and 10" << endl;
16 }
17 return 0;
18 }

Arbish Akram 10 / 19
Bitwise Operators

The & (bitwise AND) operator in C or C++ takes two numbers as


operands and performs the AND operation on every bit of the two
numbers. The result of AND is 1 only if both bits are 1.
The | (bitwise OR) operator in C or C++ takes two numbers as
operands and performs the OR operation on every bit of the two
numbers. The result of OR is 1 if any of the two bits is 1.
The ∧ (bitwise XOR) operator in C or C++ takes two numbers as
operands and performs the XOR operation on every bit of the two
numbers. The result of XOR is 1 if the two bits are different.

Arbish Akram 11 / 19
Bitwise Operators

The << (left shift) operator in C or C++ takes two numbers and left
shifts the bits of the first operand. The second operand decides the
number of places to shift.
The >> (right shift) operator in C or C++ takes two numbers and
right shifts the bits of the first operand. The second operand decides
the number of places to shift.
The ∼ (bitwise NOT) operator in C or C++ takes one number and
inverts all bits of it.

Arbish Akram 12 / 19
Unary Operators

Unary operators are operators that operate on a single operand.


Increment (++): Increases the value of the operand by 1.
int x = 5;
x++; // After this, x will be 6
Decrement (–): Decreases the value of the operand by 1.
int x = 5;
x--; // After this, x will be 4

Arbish Akram 13 / 19
Rules of Operator Precedence

1 Operators in expressions contained within pairs of parentheses are


evaluated first. Parentheses are said to be at the highest level of
precedence. In cases of nested parentheses, such as ( ( a + b ) + c )
the operators in the innermost pair of parentheses are applied first.
2 Multiplication, division and remainder operations are applied next. If
an expression contains several multiplication, division and remainder
operations, evaluation proceeds from left to right. Multiplication,
division and remainder are said to be on the same level of precedence.
3 Addition and subtraction operations are evaluated next. If an
expression contains several addition and subtraction operations,
evaluation proceeds from left to right. Addition and subtraction also
have the same level of precedence, which is lower than the precedence
of the multiplication, division and remainder operations.
4 The assignment operator ( = ) is evaluated last.

Arbish Akram 14 / 19
Rules of Operator Precedence

Arbish Akram 15 / 19
Rules of Operator Precedence

Arbish Akram 16 / 19
Precedences

Arbish Akram 17 / 19
Constants

Constants refer to fixed values that the program may not alter during
its execution. Can be defined:
1 Using define preprocessor.
2 Using const keyword.
Define constants in CAPITALS.
1 #include <iostream>
2 #define MAX_VALUE 10
3 using namespace std;
4
5 // const int MAX_VALUE = 100;
6 int main()
7 {
8 const double PI = 3.14159;
9 cout << "Max Value: " << MAX_VALUE << endl;
10 cout << "PI: " << PI << endl;
11 // MAX_VALUE = 200; // Error: Cannot modify const variable
12 return 0;
13 }

Arbish Akram 18 / 19
Type Conversion

You can convert values from one type to another explicitly using the cast
operator: (float)3

1 #include <iostream>
2 int main()
3 {
4 int a = 4, b = 3;
5 float c;
6 c = a / b; // Integer division, result will be 1, then assigned to float c
7 std::cout << "c = " << c << std::endl;
8 c = b / a; // Integer division, result will be 0, then assigned to float c
9 std::cout << "c = " << c << std::endl;
10 c = (float)a / b; // Correct conversion to float before division
11 std::cout << "c = " << c << std::endl;
12 return 0;
13 }

Arbish Akram 19 / 19

You might also like