使用输出绘制控制流图

时间:2017-12-18 15:04:14

标签: testing code-coverage control-flow-graph

我正在学习绘制控制流图,我不确定我是否正确地将其绘制为以下场景:

对于以下代码片段,其中xy是输入变量,z是输出变量:

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

这就是我所做的:

Control Flow Graph

这是否正确?提前谢谢。

1 个答案:

答案 0 :(得分:0)

否,您的控制流程图不正确。节点代表基本的代码块,这意味着它只能包含赋值语句,而不能包含条件语句或循环。边缘代表条件,例如if else else语句和case语句。

对于此问题,

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.