-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator.cpp
More file actions
122 lines (96 loc) · 4.44 KB
/
Copy pathoperator.cpp
File metadata and controls
122 lines (96 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
using namespace std;
int main() {
// ==========================================
// 1. ARITHMETIC OPERATORS
// ==========================================
int a = 10;
int b = 3;
cout << "=== 1. ARITHMETIC OPERATORS ===" << endl;
cout << "a + b = " << (a + b) << endl; // Addition (13)
cout << "a - b = " << (a - b) << endl; // Subtraction (7)
cout << "a * b = " << (a * b) << endl; // Multiplication (30)
cout << "a / b = " << (a / b) << endl; // Integer division (3)
cout << "a % b = " << (a % b) << endl; // Modulo / remainder (1)
// ==========================================
// 2. INCREMENT & DECREMENT OPERATORS
// ==========================================
int z = 5;
cout << "\n=== 2. INCREMENT & DECREMENT ===" << endl;
++z; // Increase z by 1 (z becomes 6)
cout << "After ++z : " << z << endl;
--z; // Decrease z by 1 (z becomes 5 again)
cout << "After --z : " << z << endl;
// ==========================================
// 3. ASSIGNMENT OPERATORS
// ==========================================
int x = 10;
cout << "\n=== 3. ASSIGNMENT OPERATORS ===" << endl;
x += 3; // x = x + 3 (13)
cout << "x += 3 -> " << x << endl;
x -= 3; // x = x - 3 (10)
cout << "x -= 3 -> " << x << endl;
x *= 3; // x = x * 3 (30)
cout << "x *= 3 -> " << x << endl;
x /= 3; // x = x / 3 (10)
cout << "x /= 3 -> " << x << endl;
x %= 3; // x = x % 3 (1)
cout << "x %= 3 -> " << x << endl;
// ==========================================
// 4. BITWISE ASSIGNMENT OPERATORS
// (Operates on binary bits, e.g., 5 is 0101 in binary)
// ==========================================
cout << "\n=== 4. BITWISE ASSIGNMENT (x = 5 / 0101) ===" << endl;
x = 5; x &= 3; // Bitwise AND (0101 & 0011 = 0001)
cout << "x &= 3 -> " << x << endl; // 1
x = 5; x |= 3; // Bitwise OR (0101 | 0011 = 0111)
cout << "x |= 3 -> " << x << endl; // 7
x = 5; x ^= 3; // Bitwise XOR (0101 ^ 0011 = 0110)
cout << "x ^= 3 -> " << x << endl; // 6
x = 5; x >>= 1; // Right shift: shift bits right by 1 (divide by 2)
cout << "x >>= 1 -> " << x << endl; // 2
x = 5; x <<= 1; // Left shift: shift bits left by 1 (multiply by 2)
cout << "x <<= 1 -> " << x << endl; // 10
// ==========================================
// 5. COMPARISON OPERATORS
// (Returns 1 for true, 0 for false)
// ==========================================
int c = 5;
int d = 3;
cout << "\n=== 5. COMPARISON OPERATORS (c = 5, d = 3) ===" << endl;
cout << "c == d : " << (c == d) << endl; // Equal to (0 / false)
cout << "c != d : " << (c != d) << endl; // Not equal to (1 / true)
cout << "c > d : " << (c > d) << endl; // Greater than (1 / true)
cout << "c < d : " << (c < d) << endl; // Less than (0 / false)
cout << "c >= d : " << (c >= d) << endl; // Greater than or equal to (1 / true)
cout << "c <= d : " << (c <= d) << endl; // Less than or equal to (0 / false)
// ==========================================
// 6. LOGICAL OPERATORS
// ==========================================
int val = 5;
cout << "\n=== 6. LOGICAL OPERATORS (val = 5) ===" << endl;
// && (AND): Returns true if BOTH statements are true
cout << "val < 10 && val > 2 : " << (val < 10 && val > 2) << endl; // 1 (true)
// || (OR): Returns true if AT LEAST ONE statement is true
cout << "val < 4 || val > 2 : " << (val < 4 || val > 2) << endl; // 1 (true)
// ! (NOT): Reverse the result (turns true to false and vice versa)
cout << "!(val < 10 && val > 2) : " << !(val < 10 && val > 2) << endl; // 0 (false)
// ==========================================
// 7. OPERATOR PRECEDENCE (Order of Operations)
// Priority (Highest to Lowest):
// 1. () : Parentheses
// 2. *, /, % : Multiplication, Division, Modulus
// 3. +, - : Addition, Subtraction
// 4. >, <, >=, <= : Comparison
// 5. ==, != : Equality
// 6. && : Logical AND
// 7. || : Logical OR
// 8. = : Assignment
// ==========================================
cout << "\n=== 7. OPERATOR PRECEDENCE ===" << endl;
int result1 = 2 + 3 * 4; // 2 + 12 = 14 (multiplication first)
int result2 = (2 + 3) * 4; // 5 * 4 = 20 (parentheses first)
cout << "2 + 3 * 4 = " << result1 << endl; // 14
cout << "(2 + 3) * 4 = " << result2 << endl; // 20
return 0;
}