Control Flow (Basics) Control Flow (Basics) Control Flow
Expressions
- series of computations (operators + operands)
- may produce a result
Statements
- program fragments that are evaluated in sequence
- do not produce a result
- can contain one or multiple expressions
- delimited by
;
and grouped by{ }
if (condition1) {
// do this if condition1 is true
}
else if (condition2) {
// else this if condition2 is true
}
else {
// otherwise do this
}
- code is (not) executed based on result of condition
- result of condition expression must be (convertible to) a boolean value
- conditions will be checked from top to bottom
#include <iostream>
int main () {
using std::cout;
if (true) { cout << "yes\n"; } // yes
if (false) { cout << "yes\n"; } // –
if (23) { cout << "yes\n"; } // yes (23 → true)
if (0) { cout << "yes\n"; } // – (0 → false)
int i = 0;
cout << "enter an integer: ";
std::cin >> i;
if (i < 0) {
cout << "negative\n";
} else if (i == 0) {
cout << "zero\n";
} else {
cout << "positive\n";
}
}
if(statement; condition) { … }
C++17
useful for limiting the scope of temporary variables
#include <iostream>
int main () {
int i = 0;
std::cin >> i;
if ( int x = 2*i; x > 10) { std::cout << x; }
}
- over values of integer types (
char
,int
,long
,enums
, …) - checked & executed from top to bottom
- executes everything between matching case and next break (or the closing
}
)
#include <iostream>
int main () {
int i = 0;
std::cout << "enter an integer: ";
std::cin >> i;
int m = i % 5;
switch (m) {
case 0: std::cout << i << " => case 0\n";// do this if m is 0
break;
case 1: std::cout << i << " => case 1\n";// do this if m is 1
case 3: std::cout << i << " => case 3\n";// do this (also) if m is 1 or 3
break;
default: std::cout << i << " => default case\n";// do this if m is not 0, 1 or 3
}
}
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) { … }
Result = Condition ? If-Expression : Else-Expression
#include <iostream>
int main () {
int i = 8;
int j = i > 10 ? 1 : 2;
std::cout << "i: " << i << " j: " << j << '\n';
int k = 20;
int l = (k > 10) ? 1 : 2;
std::cout << "k: " << k << " l: " << l << '\n';
int b = true;
double d = b ? 2.0 : 0.5;
double e = !b ? 2.0 : 0.5;
std::cout << "d: " << d << " e: " << e << '\n';
}
#include <iostream>
#include <vector>
int main () {
std::vector<int> v {1,2,3,4,5};
// print all elements of vector to console
for (int x : v) { std::cout << x << ' '; }
std::cout << '\n';
}
#include <iostream>
int main () {
// prints 0 1 2 3 4
for (int i = 0; i < 5; ++i) {
std::cout << i << ' ';
}
std::cout << '\n';
}
#include <iostream>
int main () {
int j = 5;
while (j < 10) {
std::cout << j << ' ';
++j;
}
std::cout << '\n';
}
as long as j < 10
prints 5 6 7 8 9
#include <iostream>
int main () {
int j = 10;
do {
std::cout << j << ' ';
--j;
} while (j > 0);
std::cout << '\n';
}
prints 10 9 8 … 1
executed until j ≤ 0
- Only write loops if there is no (standard) library function/algorithm for what you want to do (we will learn about the standard library in later chapters)!
- Prefer range-based loops over all other types of loops! (no indexing/condition bugs possible)
- Use (
do
)while
loops only, if the number of iterations is not known beforehand!