Hello everybody,, this is a simple java method that calls itself,,

class ex{
     public static void main(String [] args){
              simpleMeth(5);
 }
     static void simpleMeth(int i){
        if(i != 0){
              rec(--i);
              System.out.println(i);
        }
    }
}

If you follow the the codes you will find after sending number (5) to the method then will be tested by the if statement(line n.6) then comes again to the method(line n.7) so (i) will be 4, and will continues until (i) equals 0,,but why after ending the if statement and comes to the method end brace,returns again to the printing statement and printns (0) then goes to line n.10 and adds one to (i) and returns again to line n.8 and so until (i)equals 4 ..
the result is:
0
1
2
3
4

Hope understand what i mean :)
Thnx..

Recommended Answers

All 4 Replies

Hello everybody,, this is a simple java method that calls itself,,

class ex{
     public static void main(String [] args){
              simpleMeth(5);
 }
     static void simpleMeth(int i){
        if(i != 0){
              rec(--i);
              System.out.println(i);
        }
    }
}

If you follow the the codes you will find after sending number (5) to the method then will be tested by the if statement(line n.6) then comes again to the method(line n.7) so (i) will be 4, and will continues until (i) equals 0,,but why after ending the if statement and comes to the method end brace,returns again to the printing statement and printns (0) then goes to line n.10 and adds one to (i) and returns again to line n.8 and so until (i)equals 4 ..
the result is:
0
1
2
3
4

Hope understand what i mean :)
Thnx..

Where is rec defined?

if(i != 0){
              rec(--i);

You didn't post all of the code =/

sorry it was a mistake,,,

class ex{
     public static void main(String [] args){
              simpleMeth(5);
 }
     static void simpleMeth(int i){
        if(i != 0){
             simpleMeth(--i);
              System.out.println(i);
        }
    }
}

thnx

This is an example of recursion.

When you send the number 5 into the method simpleMeth() it will call the method simpleMeth(4) before reaching the print statement. Then simpleMeth 4 will call simpleMeth(3) before it reeaches the print statement.

This process will continue until simpleMeth reaches 0, where it will return the current status of i - in this case 0 is first printed... now that the simpleMeth(0) method is finished executing, the method before it (simpleMeth(1)) will finish executing and print 1...

This process keeps going until finally the initial method is done executing.

Keep in mind that a method has to perform functionality before execution ends. If you were to change the code such that println came before the decremented method call you would see the reverse process since the print statement occurs before the recursive method is called.

Thnx Alex for your help,,now i'm reading about it,i mean the way that recursion works,,,:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.