Pfa 6
Pfa 6
Operators
Arbish Akram
Arbish Akram 1 / 19
Operators
Arbish Akram 2 / 19
Arithmetic Operators
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
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
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
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
Arbish Akram 13 / 19
Rules of Operator Precedence
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