Control Flow (Basics)

Conditional Branching

if (statement; condition) { … } C++17

useful for limiting the scope of temporary variables

int i = 0;
std::cin >> i;
if ( int x = 2*i; x > 10) { cout << x; }

Switching: Value-Based Branching

switch (statement; variable) { … } C++17

useful for limiting the scope of temporary variables

int i = 0;
std::cin >> i;
switch (int k = 2*i; k) { … }

Loop Iteration

Range-Based Loops C++11

for (variable : range) { ... }

range = object with standard iterator interface, e.g., std::vector

std::vector<int> v {1,2,3,4,5};
// print all elements of vector to console
for (int x : v) { std::cout << x << ' '; }

References

https://hackingcpp.com/cpp/lang/control_flow_basics.html