Skip to content

Commit 5e0e13c

Browse files
Anthony YanAnthony Yan
Anthony Yan
authored and
Anthony Yan
committedDec 28, 2021
Added all materail for Section08.
1 parent 5877988 commit 5e0e13c

File tree

4 files changed

+206
-5
lines changed

4 files changed

+206
-5
lines changed
 

‎Section08-Statements_and_Operators/README.md

+88-3
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,92 @@ result = (100 == 50 + 50);
174174
result = (num1 != num2);
175175
176176
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"
178178
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

‎Section08-Statements_and_Operators/section08_coding_exercise/README.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ void assignment_operator() {
3434
//----DO NOT MODIFY THE CODE BELOW THIS LINE----
3535

3636
cout << num1 << " " << num2;
37+
3738
}
3839
```
3940

41+
4042
# Coding Exercise 8: Using the Arithmetic Operators
4143

4244
Write a program that uses arithmetic operators to manipulate an integer number that is provided to you.
@@ -82,5 +84,36 @@ int arithmetic_operators(int number) {
8284
//----WRITE YOUR CODE ABOVE THIS LINE----
8385

8486
return number;
87+
8588
}
86-
```
89+
```
90+
91+
92+
# Coding Exercise 9: Logical Operators and Operator Precedence - Can you work?
93+
94+
Write a program that determines the eligibility of an individual applying for a job as a delivery driver.
95+
96+
- driver must be `18` years of age or older
97+
- or be above the age of `15` and have their parents consent to work
98+
- they must possess a valid social security number and have no driving accidents
99+
- let the variable `age` represent the individual's age
100+
- boolean variable `parental_consent` represent parental consent
101+
- `ssn` represent a valid social security number
102+
- `accidents` represent whether they have had any car accidents
103+
104+
## Solution
105+
```c++
106+
#include <iostream>
107+
using namespace std;
108+
109+
void logical_operators (int age, bool parental_consent, bool ssn, bool accidents) {
110+
111+
//----WRITE YOUR CODE BELOW THIS LINE----
112+
113+
if ( (age >= 18 && ssn && !accidents) || (age > 15 && parental_consent && ssn && !accidents) )//WRITE ALL YOUR CODE WITHIN THE PARENTHESES
114+
cout << "Yes, you can work.";
115+
116+
//----WRITE YOUR CODE ABOVE THIS LINE----
117+
return;
118+
119+
}

‎Section08-Statements_and_Operators/section08_source_code/Challenge/main.cpp

+79
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,87 @@ using namespace std;
3939

4040
int main() {
4141

42+
// Declare changes in unit of cents
43+
const int dollar {100};
44+
const int quarter {25};
45+
const int dime {10};
46+
const int nickel {5};
47+
const int penny {1};
48+
49+
// Declare output variables
50+
int change {};
51+
int num_dollar {};
52+
int num_quarter {};
53+
int num_dime {};
54+
int num_nickel {};
55+
int num_penny {};
56+
57+
58+
cout << "Enter an amount in cents: ";
59+
cin >> change;
60+
61+
if (change == 0) {
62+
63+
cout << "\nYou can provide this change as follows: " << endl;
64+
cout << "dollars:\t0" << endl;
65+
cout << "quarters:\t0" << endl;
66+
cout << "dimes:\t\t0" << endl;
67+
cout << "nickels:\t0" << endl;
68+
cout << "pennies:\t0" << endl;
69+
70+
} else if (change < 0) {
71+
72+
cout << "Somthing is wrong... We can't have negative change." << endl;
73+
74+
} else {
75+
76+
// 1. Dollar
77+
num_dollar = change / dollar;
78+
// if (num_dollar < 1){ // didn't need these if-statements because int of decimal number is 0
79+
// num_dollar = 0;
80+
// }
81+
change -= num_dollar * dollar; // update change
82+
83+
// 2. Quarter
84+
num_quarter = change / quarter;
85+
// if (num_quarter < 1){
86+
// num_quarter = 0;
87+
// }
88+
change -= num_quarter * quarter; // update change
89+
90+
// 3. Dime
91+
num_dime = change / dime;
92+
// if (num_dime < 1){
93+
// num_dime = 0;
94+
// }
95+
change -= num_dime * dime; // update change
96+
97+
// 4. Nickel
98+
num_nickel = change / nickel;
99+
// if (num_nickel < 1){
100+
// num_nickel = 0;
101+
// }
102+
change -= num_nickel * nickel; // update change
103+
104+
// 5. Penny
105+
num_penny = change / penny;
106+
// if (num_penny < 1){
107+
// num_penny = 0;
108+
// }
109+
change -= num_penny * penny; // update change
110+
111+
// cout << change << endl; // debug to see if change equal to zero after all calculations
112+
cout << "\nYou can provide this change as follows: " << endl;
113+
cout << "dollars:\t" << num_dollar << endl;
114+
cout << "quarters:\t" << num_quarter << endl;
115+
cout << "dimes:\t\t" << num_dime << endl;
116+
cout << "nickels:\t" << num_nickel << endl;
117+
cout << "pennies:\t" << num_penny << endl;
118+
}
119+
42120
cout << endl;
43121
return 0;
44122
}
45123

46124

125+

‎Section08-Statements_and_Operators/section8_source_code/EqualityOperators/main.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,18 @@ int main() {
3737
// not_equal_result = (double1 != double2);
3838
// cout << "Comparision result (equals) : " << equal_result << endl;
3939
// cout << "Comparision result (not equals) : " << not_equal_result << endl;
40+
// if the input dobules are "12.0" and "11.99999999999", they will equate to each other becuase the representation that is used to save the doubles behind the scene is equal.
41+
// if one desired to distinguish between the two doubles, speical libraries need to be used.
4042
//
4143
// cout << "Enter an integer and a double separated by a space: ";
4244
// cin >> num1 >> double1;
4345
// equal_result = (num1 == double1);
4446
// not_equal_result = (num1 != double1);
4547
// cout << "Comparision result (equals) : " << equal_result << endl;
4648
// cout << "Comparision result (not equals) : " << not_equal_result << endl;
47-
49+
// mix mode comparison doesn't work. The two types have to be the same.
50+
// the int will be promoted to double.
51+
// the output will be equal
4852

4953
cout << endl;
5054
return 0;

0 commit comments

Comments
 (0)
Please sign in to comment.