Flow of control (control flow)
Flow of control is an order which instructions, statements and function calls are executed/evavluted in imparative program. Flow of control might be sequential i.e. executing the statements one by one (smooth control flow), conditional where program executes specific blocks based on condition (e.g. jne
in asm, if/else
in most programming languages), repetative/loops like for
, function calls and exceptional handling where program tries to recover from error or exceptions e.g. try/catch/finally
block. There can be combination of two as well like using while
or using break | continue | goto
in a loops.
Sidenote on imparative programming and declarative programming. Imparative programming is explicitly defining the steps on how to achieve the desired result. Check the example below, we are defining the steps explicitly to sum of the array elements.
std::vector v = {1, 2, 2, 3, 3, 1, 2, 2};
int sum;
for (size_t i = 0; i < v.size(); i++)
{
sum += v[i];
}
where as in Declarative programming, is defining what the result should be rather than explicit steps to achieve it. Flexibility is less, often more redable and managed by the system.
std::vector v = {1, 2, 2, 3, 3, 1, 2, 2};
int sum = std::accumulate(v.begin(), v.end(), 0);
or
select sum(impressions) from db.post;
Read about Exceptional Control Flow