@@ -174,7 +174,92 @@ result = (100 == 50 + 50);
174
174
result = (num1 != num2);
175
175
176
176
cout << (num1 == num2) << endl; // 0 or 1
177
- cout << std::boolalpha;
177
+ cout << std::boolalpha; // this allow display "true or false" rather than "0 or 1"
178
178
cout << (num1 == num2) << endl; // true or false
179
- cout << std::noboolalpha;
180
- ```
179
+ cout << std::noboolalpha; // this revert display "true or false" back to "0 or 1"
180
+ ```
181
+
182
+ ## Relational Operator
183
+
184
+ | Operator | Meaning |
185
+ | --- | --- |
186
+ | ` > ` | greater than |
187
+ | ` >= ` | greater than or equal to |
188
+ | ` < ` | less than |
189
+ | ` <= ` | less than or equal to |
190
+ | ` <=> ` | three-way comparision (C++20) |
191
+
192
+
193
+ ## Logical Operators
194
+
195
+ | Operator | Meaning |
196
+ | --- | --- |
197
+ | ` not ` , ` ! ` | negation |
198
+ | ` and ` , ` && ` | logical and |
199
+ | ` or ` , `|| ` | logical or |
200
+
201
+
202
+ ### AND or ` && `
203
+
204
+ - is only true if both operands are true
205
+
206
+ | Expression A | Expression B | ` A and B ` or ` A && B ` |
207
+ | --- | --- | --- |
208
+ | true | true | ** true** |
209
+ | true | false | false |
210
+ | false | true | false |
211
+ | false | false | false |
212
+
213
+
214
+ ### OR or ` || `
215
+
216
+ - is true if one or more operands are true
217
+
218
+ | Expression A | Expression B | ` A or B ` or `A || B` |
219
+ | --- | --- | --- |
220
+ | true | true | ** true** |
221
+ | true | false | ** true** |
222
+ | false | true | ** true** |
223
+ | false | false | false |
224
+
225
+
226
+ ### Precedence
227
+
228
+ - ` not ` has higher precedence than ` and `
229
+ - ` and ` has higher precedence than ` or `
230
+ - ` not ` is a unary operator
231
+ - ` and ` and ` or ` are binary operators
232
+
233
+
234
+ ### Short-Circuit Evaluation
235
+
236
+ - When evaluating a logical expression C++ stops as soon as the result is known
237
+
238
+
239
+ ## Compound assignemnt
240
+
241
+ | Operator | Example | Meaning |
242
+ | --- | --- | --- |
243
+ | ` += ` | lhs += rhs; | lhs = lhs + (rhs); |
244
+ | ` -= ` | lhs -= rhs; | lhs = lhs - (rhs); |
245
+ | ` *= ` | lhs * = rhs; | lhs = lhs * (rhs); |
246
+ | ` /= ` | lhs /= rhs; | lhs = lhs / (rhs); |
247
+ | ` %= ` | lhs %= rhs; | lhs = lhs % (rhs); |
248
+ | ` >>= ` | lhs >>= rhs; | lhs = lhs >> (rhs); |
249
+ | ` <<= ` | lhs <<= rhs; | lhs = lhs << (rhs); |
250
+ | ` &= ` | lhs &= rhs; | lhs = lhs & (rhs); |
251
+ | ` ^= ` | lhs ^= rhs; | lhs = lhs ^ (rhs); |
252
+ | `| =` | lhs | = rhs; | lhs = lhs | (rhs); |
253
+
254
+ ``` c++
255
+ a += 1 ; // a = a + 1;
256
+ a /= 5 ; // a = a / 5;
257
+ a *= b + c; // a = a * (b + c);
258
+
259
+ cost += items * tax; // cost = cost + (items * tax);
260
+ ```
261
+
262
+ ## Operator Precedence
263
+
264
+ - [ C++ Operator Precedence] ( https://en.cppreference.com/w/cpp/language/operator_precedence )
265
+ - Use parenthesis is good practice to remove confusion
0 commit comments