-
Notifications
You must be signed in to change notification settings - Fork 1
/
control_flow.hpp
244 lines (191 loc) · 4.61 KB
/
control_flow.hpp
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#ifndef __CONTROL_FLOW_H
#define __CONTROL_FLOW_H
#include <iostream>
#include <iomanip>
#include <ios>
#include <cstdlib>
#include <bitset>
/*
- Short Circuit Evaluation:
- (&&) == (AND) :
- If one of the operands is 'false':
- The result is 'FALSE'.
- Put operands likely to be 'false' first.
- (||) == (OR) :
- If one of the operands is 'true':
- The result is 'TRUE'.
- Put operands likely to be 'true' first.
*/
void short_circuit_evaluation()
{
bool
a = true,
b = true,
c = true,
d = false;
bool
v = false,
x = false,
y = false,
z = true;
bool result;
std::cout << std::boolalpha;
// AND : if one of the operands 'false', the result is false
std::cout << "AND SHORT CIRCUIT: " << std::endl;
result = a && b && c && d; // result => false, because (d) is false
std::cout << "result: " << result << std::endl;
// OR : if one of the operands 'true', the result is true
std::cout << "OR SHORT CIRCUIT: " << std::endl;
result = v || x || y || z; // result => true, because (z) is true
std::cout << "result: " << result << std::endl;
}
/*
- Itegral Logic Conditions
- (0) is always false.
- (1) is always true.
- (Any number positive or negative except zero) will be alwys true.
- ex:
-2 = true
-9 = true
0 = false
1 = true
15 = true
*/
void itegral_logic_conditions()
{
// int condition { 0 }; // Represents FALSE anyway.
int condition { -10 }; // Represents TRUE anyway.
std::cout << std::boolalpha;
if(condition)
{
std::cout
<< "The Condition is: "
<< static_cast<bool>(condition) // using explicit type conversion
<< std::endl
<< "The Condition is TRUE and has a value of: "
<< condition
<< std::endl;
}
else
{
std::cout
<< "The Condition is: "
<< static_cast<bool>(condition) // using explicit type conversion
<< std::endl
<< "The Condition is FALSE and has a value of: "
<< condition
<< std::endl;
}
}
/*
- Ternary Operators:
- result = (eval_expr) ? if_true_do : else_do;
- Ternary Initialization:
- var { (eval_expr) ? if_true_do : else_do };
*/
void ternary_ops()
{
unsigned int a { 65 }, b { 71 }, result;
result = (a > b) ? a : b;
std::cout
<< "a value is: "
<< a
<< std::endl
<< "b value is: "
<< b
<< std::endl
<< "a > b result is: "
<< (
(a > b) ?
"The value of a is greater than b." :
"The value of b is greater than a."
)
<< std::endl
<< std::setw(20)
<< "The result is: "
<< result
<< std::endl;
}
void ternary_init()
{
int normal_speed = 100; // On normal speed
int fast_speed = 200; // On fast speed
bool accelerate = true; // Modifying the speed
// Using ternary initialization
int speed { (accelerate) ? fast_speed : normal_speed };
std::cout
<< "Speed is: "
<< speed
<< ( (accelerate) ? " on fast speed" : " on normal speed" )
<< std::endl;
}
/*
- if constexpr
- Do conditional at compile-time.
- The evaluated expression should be fully evaluated at compile-time:
- const bool condition { true };
- constexpr bool condition { true };
*/
void if_constexpr()
{
const bool condition { false };
// const bool condition { true };
std::cout << std::boolalpha;
if constexpr (condition)
{
std::cout
<< "if constexpr evaluated as: "
<< condition
<< std::endl;
}
else
{
std::cout
<< "if constexpr evaluated as: "
<< condition
<< std::endl;
}
}
/*
- if with initializer:
- This enhancement simplifies common code patterns and helps users keep scopes tight.
- Which in turn avoids variable leaking outside the scope.
- if (initialize; condition) { } else { }
*/
void if_with_initializer()
{
srand(time(NULL));
// C++17 if statement with initializer
if (int random_num = rand(); random_num % 2 == 0) {
std::cout << random_num << " is an even number\n";
} else {
std::cout << random_num << " is an odd number\n";
}
}
/*
- switch with initializer:
- switch (initialize; condition)
*/
void switch_with_initializer()
{
struct Device
{
enum State { SLEEP, READY, BAD };
auto state() const { return m_state; }
private:
State m_state{};
};
switch (auto dev = Device{}; dev.state())
{
case Device::SLEEP:
std::cout << "SLEEP" << std::endl;
break;
case Device::READY:
std::cout << "READY" << std::endl;
break;
case Device::BAD:
std::cout << "BAD" << std::endl;
break;
}
}
#endif