Control Flow Graph – How to Create a Control Flow Graph for a Function

control flowdesigngraph

Given is a short Java function and I like to create a control flow graph for it but I'm not sure if it's fine like that? Because I left some things away such as variables that have already been created with the function together (int[] A, boolean[] boo).

boolean func(int[] A, boolean[] boo){
    boolean res;
    int n, leng;
    leng = A.length;
    n = 0;
    res = true;
    while(n < leng){
        if(A[n] <= 0 && !boo[n]){
            res = false;
        }
        n++;
    }
    return res;
}

enter image description here

Link to the chart

Is it fine like that? Because in the test I write soon I would do it like that : /

Best Answer

Control flow graphs are usually not written with one statement per node, but divide the code into basic blocks that combine multiple statements. A basic block starts with a jump target and ends with a jump to another block. The jump at the end may be conditional.

Your control flow graph is generally correct, except

  • that it could be simplified by using basic blocks
  • that the n++ node has multiple outgoing jumps, but isn't a conditional statement. There must not be a direct jump from n++ to return res, instead there should only be the jump back to the while (...).
Related Topic