HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Control flow graphs - Tree decomposition

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
flowcontrolgraphsdecompositiontree

Problem

Considering above terminologies for drawing control flow graphs for any program, it is very simple. For example :

While A
if B
do ..
else do ..
end while


For above example, while doing decomposition, I can say its


D2 (D1)

i.e while and then inside while, its if-then-else.

Considering same situation. How can you represent


CONTINUE and BREAK statements

Ever for FOR statement there is no defined terminology like for while and if then else. FOR statement falls under while.

My prof says in theory, there is nothing about Break and continue statement and I couldnt find anything online too.

For example :

# include 
int main(){
   float num,average,sum;
   int i,n;
   printf("Maximum no. of inputs\n");
   scanf("%d",&n);
   for(i=1;i<=n;++i){
       printf("Enter n%d: ",i);
       scanf("%f",&num);
       if(num<0.0)
       break;                     //for loop breaks if num<0.0
       sum=sum+num;
}
  average=sum/(i-1);       
  printf("Average=%.2f",average);
  return 0;
}


Control flow graph for this is as below: the nodes has line number written : (Sorry the image is side ways)

I decomposed this as :

P1;P1;D2(P1;P1;D1);P1

* P1 represents set of statements outside loops


I'm not sure if this is correct. My professors says to use something as D22 for break, like create a new term from above image.


My main question here is the decomposition. I Know that i drew the
CFG correctly, but is the decomposition correct according to the first
image?. The break state kind of creates a while as you can see in CFG,
but i'm not sure if it has to be considered as while and I guess we
cannot, as per my professor.

I am working on this and wanted to know, if anyone has come across something for Break and continue statements while decomposition of graphs, please let me know.

Thanks.

PS : Please, Let me know, if am unclear and if anymore info is needed. I can probably write down an example and upload the picture.

Solution

There are many different kinds of control-flow constructs that only map to "structured programming" (tree decomposition) constructs when you add extra data variables and extra tests. continue is usually okay (as long as its not nested any deeper), but break is one of the hard ones. Another relatively painful case is short-circuit conditional evaluation.

A preprocessing step of converting while x stmt to if x repeat stmt until not(x) will make everything a little easier (and you want to do this anyway, because it lets you safely optimize loop invariants). Example:

while i<=n:
  something
  if condition break
  something_else
  i = i+1


becomes

if i<=n:
  repeat:
    something
    if condition break
    something_else
    i = i+1
  until not(i<=n)


A second preprocessing step of saving the (possibly complicated) calculation of the loop condition as a boolean temporary variable will also help. So:

if i<=n:
  repeat:
    something
    if condition break
    something_else
    i = i+1
    t0 = not(i<=n)
  until t0


Now we can convert our break into a if-else:

if i<=n:
  repeat:
    something
    if condition:
      t0 = false
    else:
      something_else
      i = i+1
      t0 = not(i<=n)
  until t0


So we've converted, but the result is kind of ugly: we now have to keep track of t0 and we have two tests where we only had one before. "Structured programming" is not the panacea it is often made out to be.

Code Snippets

while i<=n:
  something
  if condition break
  something_else
  i = i+1
if i<=n:
  repeat:
    something
    if condition break
    something_else
    i = i+1
  until not(i<=n)
if i<=n:
  repeat:
    something
    if condition break
    something_else
    i = i+1
    t0 = not(i<=n)
  until t0
if i<=n:
  repeat:
    something
    if condition:
      t0 = false
    else:
      something_else
      i = i+1
      t0 = not(i<=n)
  until t0

Context

StackExchange Computer Science Q#23905, answer score: 5

Revisions (0)

No revisions yet.