0

I indicate the base case as emptySpace=0 and the else if condition has the base case as star=0. I was expecting that the program would print the spaces first after it prints the stars but the opposite situation happens. Shouldn't it print the spaces first then stars?

public static void displayStarss(int emptySpace, int star) {
    if (emptySpace != 0) {
         displayStarss(emptySpace - 1, star);
         System.out.print(" ");
    } else if (star != 0) {
        displayStarss(emptySpace, star - 1);
        System.out.print("*");
    }
}

public static void main(String[] args) {
    displayStarss(3, 3);
}
2
  • 3
    How do you call it? Pick low numbers and "play computer" with a pencil and paper, or step through the code. Commented Jul 8, 2019 at 15:33
  • Also, watch what happens if you move the recursive call one line lower in each branch and have the print statement before you step into the recursion. Commented Jul 8, 2019 at 15:34

3 Answers 3

2

It follows your orders on which order it should do things:

displayStarss(3, 3);
-> displayStarss(2, 3);
-> -> displayStarss(1, 3);
-> -> -> displayStarss(0, 3);
-> -> -> -> displayStarss(0, 2);
-> -> -> -> -> displayStarss(0, 1);
-> -> -> -> -> -> displayStarss(0, 0);
-> -> -> -> -> System.out.print("*");
-> -> -> -> System.out.print("*");
-> -> -> System.out.print("*");
-> -> System.out.print(" ");
-> System.out.print(" ");
System.out.print(" ");
Sign up to request clarification or add additional context in comments.

Comments

0

Let us say we call this with parameters 2 for both emptySpace and stars

The call goes as

displayStarss(2, 2) //called from main (starting point)

displayStarss(1, 2) //called from if
displayStarss(0, 2) //called from if

displayStarss(0, 1) //called from else if
displayStarss(0, 0) //called from else if
<<recursion ends here and it returns>>

Now, if you unwind the above call sequence, it can be seen that it must print the stars before the spaces.

Comments

0

It doesn't. It may do it in order. It all depends on how you structure the program. Consider the following:

   public static void count(int n) {
      // when n == 0 start the return process
      if (n == 0) {
         return;
      }
      // Nothing has been printed yet
      // Call count again, this time with one less than n.
      count(n - 1);
      // all returns will end up here, restoring each previously
      // altered value of n.
      System.out.println(n);
   }

This program will print all the values from 1 to n. That is because the print statement is after the recursive call to count, each time specifying one less than n. So the call stack keeps these values and returns them in order.

If you move the print statement right before the call to count, the values will be printed from n to 1. In this latter case, the returns still happen, but nothing is being done so they just return in succession until the method returns to the original caller.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.