Skip to content

Commit 7198409

Browse files
committed
Added materail for Section08 thus far: Assignment, Arithmetic, and Euros.
1 parent c04ae44 commit 7198409

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed

Section08-Statements_and_Operators/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,11 @@
7979
- assignment expressions is evaluated to what was just assigned
8080
- more than one variable can be assigned in a single statement
8181

82+
## Arithmetic Operators
83+
84+
- Addition (`+`)
85+
- Subtraction (`-`)
86+
- Multiplication (`*`)
87+
- Division (`/`)
88+
- Modulo or remainder (`%`)
89+
- only works with integers

Section08-Statements_and_Operators/section8_source_code/ArithmeticOperators/main.cpp

+16-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ int main() {
1919
int num1 {200};
2020
int num2 {100};
2121

22-
// cout << num1 << " + " << num2 << " = "<< num1+ num2 << endl;
22+
cout << num1 << " + " << num2 << " = "<< num1+ num2 << endl;
2323

2424
int result {0};
2525

@@ -37,21 +37,32 @@ int main() {
3737

3838
result = num2 / num1;
3939
cout << num2 << " / " << num1 << " = "<< result << endl;
40-
40+
// since we are working with integers the 5 in 0.5 will be chopped and only 0 is outputted
41+
// use double instead of integer type
42+
4143
result = num1 % num2;
42-
cout << num1 << " % " << num2 << " = "<< result << endl;
44+
cout << num1 << " % " << num2 << " = "<< result << endl;
45+
// 200 divided by 100 = 2 with no reamainder, remainder = 0, result = 0
4346

4447
num1 = 10;
4548
num2 = 3;
4649

4750
result = num1 % num2;
48-
cout << num1 << " % " << num2 << " = "<< result << endl;
51+
cout << num1 << " % " << num2 << " = "<< result << endl;
52+
// 10 divided by 3 = 9 with reaminader = 1, result = 1
4953

50-
result = num1 * 10 + num2;
54+
result = num1 * 10 + num2;
55+
// compiler might read right to left but arithmetic operations still holds true
56+
// num1 will be multiplied by 10 first, then add num2
57+
58+
// if one desire to multiply num1 by 10 + num2, then we need perentesis ()
59+
result = num1 * (10 + num2);
5160

5261
cout << 5/10 << endl;
62+
// output 0 since 0.5 is not an integer, the 5 will be chopped
5363

5464
cout << 5.0/10.0 << endl;
65+
// output 0.5 since we are using double instead of integer with the .0
5566

5667
cout << endl;
5768
return 0;

Section08-Statements_and_Operators/section8_source_code/AssignmentOperator/main.cpp

+24-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,37 @@ using namespace std;
77

88
int main() {
99

10+
// variables initialization
1011
int num1 {10};
1112
int num2 {20};
1213

13-
num = 100;
14-
14+
// assignment statement:
15+
num1 = 100; // assign 100 into num1
16+
// num1 = num2; // assign num2 value into num1
17+
// num1 = num2 = 1000; // works since compiler reads from right to left
18+
// num1 = "Frank"; // compiler will try to put string type into int type but will fail and output "conversion error"
19+
1520
cout << "num1 is " << num1 << endl;
1621
cout << "num2 is " << num2 << endl;
1722

1823
cout << endl;
1924
return 0;
2025
}
2126

27+
/*
28+
int main() {
29+
30+
// variables initialization
31+
const int num1 {10};
32+
int num2 {20};
33+
34+
// num1 = 100; // this doesn't work because num1 is a constant, compiler will throw "read-only variable error"
35+
// 100 = num1; // compiler doesn't understand and throw error "lvalue required as left operand of assignment"
36+
37+
cout << "num1 is " << num1 << endl;
38+
cout << "num2 is " << num2 << endl;
39+
40+
cout << endl;
41+
return 0;
42+
}
43+
*/

Section08-Statements_and_Operators/section8_source_code/Euros/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ int main() {
1010
const double usd_per_eur {1.19};
1111

1212
cout << "Welcome to the EUR to USD converter" << endl;
13-
cout << "Enter the value in EUR : ";
13+
cout << "Enter the value in EUR: ";
1414

1515
double euros {0.0};
1616
double dollars {0.0};

0 commit comments

Comments
 (0)