Control Flow Graph (CFG) Software Engineering (original) (raw)

Last Updated : 11 Jul, 2025

A **Control Flow Graph (CFG) is the graphical representation of control flow or computation during the execution of programs or applications. Control flow graphs are mostly used in static analysis as well as compiler applications, as they can accurately represent the flow inside a program unit. The control flow graph was originally developed by _Frances E. Allen.

Characteristics of Control Flow Graph

There exist 2 designated blocks in the Control Flow Graph:

Hence, the control flow graph comprises all the building blocks involved in a flow diagram such as the start node, end node, and flows between the nodes.

General Control Flow Graphs

Control Flow Graph is represented differently for all statements and loops. The following images describe it:

1. If-else

if-else cfg

2. **While

while cfg

3. **do-while

do while cfg

4. **for

for cfg

Example

if A = 10 then

if B > C

A = B

else A = C

endif

endif

print A, B, C

Flowchart of above example will be:

control-flow-graph-example

control flow graph

Control Flow Graph of above example will be:

cfg_example

control flow graph

Advantage of CFG

Disadvanatges of CFG

Conclusion

Control Flow Graph (CFG) is a helpful tool to understand how a program works. It makes it easier to find errors, improve performance, and ensure thorough testing. While it can get complex for large programs, it’s still a valuable way to analyze and optimize code effectively.