-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.cpp
More file actions
56 lines (47 loc) · 1.41 KB
/
control.cpp
File metadata and controls
56 lines (47 loc) · 1.41 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
#include <iostream>
using namespace std;
int main() {
// if something is true, do this; else do something else
if (true) {
cout << "\"true\" is true" << endl;
}
if (0) {
cout << "0 is true" << endl;
} else {
cout << "0 is false" << endl;
}
// comparison operators: == != > < >= <=
if (2 > 1) cout << "2 is greater than 1" << endl; // note - 1 statement, don't need {}
// Using = instead of == A common practice but be careful
int my_six = 6;
if ((my_six = 7)) { // extra parens supresses commpiler warning
cout << "6 is equal to 7" << endl;
} else {
cout << "6 is not equal to 7" << endl;
}
// for loops: doing something multiple times with a counter
int value = 10;
int answer = 1;
for (int i = 1; i <= value; i++) {
answer *= i;
}
cout << "Factorial of " << value << " is " << answer << endl;
// while loop - keep doing something while a some condition is true
value = 10;
answer = 1;
int original_value = value; // why do we need a copy of the original value?
while (value > 1) {
answer *= value--;
}
cout << "Factorial of " << original_value << " is " << answer << endl;
// do-while: do something at least once, and keep going while some condition is true
value = 10;
answer = 1;
do {
answer *= value--;
}
while (value > 1);
cout << "Factorial of " << original_value << " is " << answer << endl;
// End of main()
return 0;
}