Control Flow Graph

A program while executing may choose one of the many paths depending on the input or inputs provided. A control flow graph is a pictorial representation of all the feasible and infeasible paths that exist in a program. A control flow graph is a representation of combination of all the paths that a program can take while it's execution. It describes the flow of program that can determine the initial statement, final statement and set of predecessors and successors of other statements.

Example 1

void greater(int x)
{
int y=10;
if(x>y)
System.out.println("GREATER");
else
System.out.println("LESSER");
}

 


Init is the initial statement(program entry point). When there are multiple final statements, they are all connected to an explicitly created last node(like in example above). We can also combine all the sequential statements in a CFG to form a basic block with single entry and single exit.

 
Example 2
void reverse(int x)
{
int y=x;
while(y!=0)
{
System.out.print(y%10);
y=y/10;
}
}