0

I'm currently learning to draw Control Flow Graphs and I'm not sure whether I'm drawing it right for the following scenario:

For the following fragment of code, where x and y are the input variables and z is the output variable:

if x=0 then x:=10
if x<y-5 then y:=y-1 else x:=y+5
z:=y-x

This is what I've done:

Control Flow Graph

Is this correct in any way? Thanks in advance.

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
ArnoldR
  • 13
  • 3

1 Answers1

0

No, your control flow diagram is not correct. Nodes represent basic blocks of code which means it can contain only assignment statements, no conditional statements or loops. Edges represent conditions such as if then else statements and case statements.

For this problem,

1. Start node is the first node with an incoming arrow. So, remove the solid node at the top and leave the arrow as it is.
2. Replace true in first left branch with x == 0
3. Replace false in the first right branch with x != 0 and connect it to the node containing x < y-5. Remove the solid node.
4. Replace true following node containing x < y-5 with x < y-5
5. Replace false following node containing x < y-5 with x >= y-5
6. Remove last two true on the edges
7. Make the node containing z:=y-x as last node and remove the arrow and solid node after this node.