Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6184141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:29:06+00:00 2026-05-24T01:29:06+00:00

Ok so i am just learning recursion and i am confused on one point.

  • 0

Ok so i am just learning recursion and i am confused on one point.
This is the code

public class RecursiveDemo {
  public static void showRecursion (int num) {
   System.out.println("Entering method. num = " + num);
    if (num > 1) {
      showRecursion(num - 1);
    }
    System.out.println("Leaving method. num = " + num);
  }

  public static void main(String[] args){
    showRecursion(2);
  }
}

The output i am getting is :

Entering method. num = 2
Entering method. num = 1
Leaving method. num = 1
Leaving method. num = 2

My question is why am i getting the output “Leaving method. num = 2“. Shouldn’t it stop at “Leaving method. num = 1” since num has already reached 1?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:29:06+00:00Added an answer on May 24, 2026 at 1:29 am

    Once the original invocation of this method leaves the if statement, it passes to System.out.println("Leaving method. num = " + num);. Since you invoked the message originally with value 2, 2 is the value of num for this part of the code.

    Your code runs like this (pseudocode):

    Start First Call
        if statement
           Start Second call
               Skips if statement
               Print from Second Call
           End of Second Call
        End of if Statement
        Print From First Call
    End of First Call
    

    It looks like you have a fundamental misunderstanding of recursion.
    When you call your method with (num-1) as arguments, the parent call (the first call, in this case), retains the value num as its argument, which is 2, in this case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(just learning MVC) I have created a model class: public class Employee { public
I am just learning about foreign keys. I am simplifying my tables to this:
I'm just learning python and confused when a def of a function ends? I
I just started learning C++ couple of weeks ago. So now I have this
Im just learning javascript and I'm just wondering why this doesn't work. I've created
just learning as3 for flex. i am trying to do this: var someNumber:String =
Just learning php and looking into someone else's code. I'm not sure what is
Just learning R. Given a data.frame in R with two columns, one numeric and
Am just learning C and this is my first time on stackoverflow so am
Just learning Map/Reduce and I'm missing a step. I've read this post ( RavenDB

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.