0

This works fine because we are using the prefix decrement operator in the recursive call !

public static void main(String[] args) {
    doubt1(5);
}

Prefix Decrement:

static void doubt(int num)
{
    if(num==0)
        return;
    System.out.println(num);
    doubt(--num);
}

Postfix Decrement:

static void doubt1(int num)
{
    if(num==0)
        return;
    System.out.println(num);
    doubt1(num--);
}

But when we use the postfix decrement it can lead to stack over flow error , I understand why it's causing the error , but I want to know , if we use num-1 in the recursive call it doesn't give the overflow error , my question is -> why it's not giving the error coz it calculating the num-1 and passing the value to the recursive call but not calculating the num-- in the call ?

Want the explanation why it calculating the num-1 not num-- in the recursive call ? Not why it's happening !

1
  • Lets say num is 5. doubt(num--) is same as calling doubt(5), whereas both of doubt(num-1) and doubt(--num) are the same as calling doubt(4) and num inside the nested call is now 4... and so on Commented May 20, 2023 at 14:49

3 Answers 3

2
    doubt1(num--);

is internally implemented as

    int tmp = num;
    num = num - 1;
    doubt1(tmp);

That is, it recursively calls the doubt1 method with the original value and therefore the condition num == 0 is never reached.

Sign up to request clarification or add additional context in comments.

Comments

0

When you invoke a method, the arguments are evaluated, and the resulting values are assigned as the initial values of the method's parameters. The expressions num-- and --num have the same side effect on num, but that is immaterial to the method invocation. The two evaluate to different values: the former to the value of num before it is decremented, and the latter to the value of num after it is decremented. The order of the operator relative to the operand is mnemonic for that.

Thus, with

int n = 1;
some_method(n--);

the value passed to some_method() is 1, but with

int n = 1;
some_method(--n);

the value passed to some_method() is 0.

In both cases, however, the value 0 is stored in n before execution of the body of some_method() commences.

Comments

0

First, with the unary decrement operator, the operation is occurring after, or before, the statement.
As opposed to after, or before, the expression.

So, in this situation, there will be a difference between num-- and --num.

  • doubt(num--) is decrementing after the call statement, and not after the parameter expression.

  • double(--num), the opposite is happening, the decrement is happening before the parameter expression.

Essentially, from a compiler's stand-point, for the doubt(--num) statement, you have the following.

  1. Get num
  2. Decrement num
  3. Apply num as doubt parameter
  4. Execute doubt with parameter value

Alternately, with doubt(num--) you have the following.

  1. Get num
  2. Apply num as doubt parameter
  3. Decrement num (which has no effect, since the parameter value was already set)
  4. Execute doubt with parameter value

Essentially, for the doubt(num--) statement, after the compiler has num, and is ready to call doubt, it will then decrement num.
As opposed to having num, then decrementing, and then calling doubt.

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.