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

Execution flow diagram (call-stack) of recursive count-down

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
flowstackcalldiagramrecursivedowncountexecution

Problem

I have this code:

public class RecurLoopTest {

    public static void main(String[] args) {

     printit(2);    

    }

    private static int printit(int n){

        if(n>0){
            printit(--n);
        }

      System.out.print(n+",");

      return n;
   }
}


I have drawn a execution flow/memory flow diagram for above program. Is this diagram correct or do I need some changes to make it correct?

What I have drawn looks like this:

Link to edit diagram

Link to view diagram

Solution

It would be better to add the method arguments in the diagram, for example:

printit(0) -- n=0, prints "0,", returns 0
printit(1) -- n=1, prints "0,", returns 0
printit(2) -- n=2, prints "1,", returns 1
main()


I don't think you need 4 drawings side by side for this, just one like this would be clear enough already.

Code Snippets

printit(0) -- n=0, prints "0,", returns 0
printit(1) -- n=1, prints "0,", returns 0
printit(2) -- n=2, prints "1,", returns 1
main()

Context

StackExchange Code Review Q#47691, answer score: 2

Revisions (0)

No revisions yet.