How do I draw Control Flow Graph from this code? I'll appreciate if someone can show the process. I am able to draw CFG from a very simple cases but I cannot do this one. Also I need to determine the basis paths and the tests for Multiple Condition criteria. It's not a homework question, I just try to understand the course material. Thanks.
749k 144 144 gold badges 940 940 silver badges 1.3k 1.3k bronze badges asked Jun 18, 2018 at 16:20 LearningMath LearningMath 851 4 4 gold badges 17 17 silver badges 40 40 bronze badges Are you sure this is Java and not C? Commented Jun 29, 2018 at 13:15 Oh, I'm sorry I've added Java, it is not, it's C but that is irrelevant. Commented Jun 29, 2018 at 13:42Here's the definition of CFG from Wikipedia, I know you already know this but for the sake of completeness I'm putting it here
A control flow graph (CFG) in computer science is a representation, using graph notation, of all paths that might be traversed through a program during its execution.
Following is the definition of a Path
Path: a sequence of node on the CFG (static), including an entry node and an exit node; path segment: a subsequence of nodes along the path
So the reason for drawing one would be to determine all possible paths taken by the program, which may help us determine things like test coverage without actually running the program (static analysis).
Following are the simple rules that we can follow to draw a CFG
Here's a cheat sheet which explains it better
Now lets map every statement in your program to an number that we'll use to denote CFG nodes
int main() < 1. int i, grade = 0; 2. printf (" Enter points: \n"); 3. scanf ("%d", &i); 4. if (i >= 50 && i 50 && i 60 && i 70 && i 80 && i 90 && i = 1 && p = 8 || p == 0)) 23. sign = '+'; > 24. printf (" The grade is %d%c. \n", grade, sign); > 25. return 0; >
Here's the output created by following the directions from cheat sheet diagram above. Notice that node 16 and 24 are acting as join node for many conditional nodes before.
Credit: I have used draw.io to create images posted above.
Note: Secret to drawing a CFG is to treat every statement independent to the program, draw it and then link it's entry and exit to the rest of the graph.
Following are a few initial steps that I followed
And so on, we keep checking the cheat sheet for the applicable nodes and create them in isolation then link then with previous nodes.