A control flow path (or control flow graph) is a graphical representation of the flow of a program.
Control flow paths represent all of the paths we can traverse during the execution of a program.
We use control flow paths most frequently for the white box testing of software.
A control flow graph is a directed graph.
A control flow graph consists of two designated blocks:
As their names suggest, these blocks direct the entry and exit of the control flow into and out of the graph.
A decision point is a point in the control flow graph where the control of the program diverges. Common examples include do-while
statements, if
statements, and case
statements.
These statements are also referred to as control flow statements.
We can better understand control flow paths by making one of our own.
Consider the following program:
#include <iostream>using namespace std;int main(){int a[100] = {0};for(int i = 0; i < 100; i++)a[i] = i;int temp;int x = 0;while (x < 100){if (a[x] % 2 == 0)temp = 0;elsetemp = 1;switch (temp){case 0: cout << "a["<<x<<"] is even\n"; break;case 1: cout << "a["<<x<<"] is odd\n"; break;default: cout << "unexpected error\n";}x++;}return 0;}
The above program declares and populates an array of size 100. Then, we check each array element to determine whether it is even or odd.
Based on that, we set the value of temp
(0 for even, 1 for odd).
For each element, the switch
statement runs the case
based on the value of temp
.
Finally, the value for x
is incremented after breaking out of the switch statement.
The control flow graph for the program is as follows:
The following is the breakdown of the control flow graph above:
The start of the while
loop
The if
statement
The branch we take if the if
condition is true
The else
statement
The switch
statement
case 0
statement
case 1
statement
default
statement
Increment (x++
)
return
statement
Free Resources