Vy Do
Vy Do

Reputation: 52516

Explaining about prints output ordering of recursive loop?

Can anyone please explain the print order of the recursive loop?

import java.util.Scanner;

public class DecimalToBinary {
    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {
        int decimalNum;
        int base;
        base = 2;
        System.out.println("Enter a nonnegative integer in decimal: ");
        decimalNum = console.nextInt();
        System.out.println();
        System.out.println("Decimal " + decimalNum + " = ");
        decToBin(decimalNum, base);
        System.out.println(" binary");
    }

    public static void decToBin(int num, int base) {
        if (num == 0) {
            System.out.print(0);
        } else if (num > 0) {
            decToBin(num / base, base);
            System.out.print(num % base);
        }
    }
}


Num % base must print reverse order like this:

enter image description here


why is the order of calls as shown? (Please help me revise my question, English is my foreign language)

Upvotes: 0

Views: 127

Answers (2)

call me carrot
call me carrot

Reputation: 192

the order of the output is reversed because once the dectobin function is called

   decToBin(int num, int base) {
    if (num == 0) {
        System.out.print(0);
    } else if (num > 0) {

it reaches the line

    decToBin(num / base, base);

where it postpones its execution and calls "another instance" of the dectobin function with decreased number parameter, before getting a chance to output anything(in the code below)

        System.out.print(num % base);
    }

then this subsequent call of dectobin is stopped at the same line and another "instance" is started with even smaller num. and so on and so on. none of the "instances" so far gets a chance to print anything.

at some point the "instance" of the function which was started last, recognizes that its num argument has decreased under value of 1; and since num is integer type, once it is positive but less than 1 it is "Rounded" to 0. so that the following condition is true:

    if (num == 0) {
        System.out.print(0);

then this last instance behaves differently from all its predecessors. instead of postponing its execution and creating a new "instance" it prints '0' in the line above and just ends returning the execution point to the one "instance" which called it, which then continues to run from the line it was postponed. then this "instance" outputs its number

     system.out.print(num % base);

and ends itself returning the execution to the one which was starting it. and so and so on.

the bottom line is: the function "instance" which started last had the first output.the one which started first had the last

Upvotes: 1

Mshnik
Mshnik

Reputation: 7032

Your printing occurs after the recursion. Using (25, 2) as an example, the order of your calls with printing looks like

decToBin(25, 2):
    decToBin(12,2):
        decToBin(6,2):
            decToBin(3,2):
                decToBin(1,2):
                    decToBin(0,2):
                        print(0)
                    print(1%2)
                print(3%2)
            print(6%2)
        print(12%2)
    print(25%2)

Removing the recursive calls and just leaving the print statements shows the order you are getting:

decToBin(25, 2):
    print(0)
    print(1%2)
    print(3%2)
    print(6%2)
    print(12%2)
    print(25%2)

If you want the printing to be in the reverse order, move the print statement before the recursive call:

public static void decToBin(int num, int base) {
    if (num == 0) {
        System.out.print(0);
    } else if (num > 0) {
        System.out.print(num % base);
        decToBin(num / base, base);
    }
}

New recursion with printing:

decToBin(25, 2):
    print(25%2)
    decToBin(12,2):
        print(12%2)
        decToBin(6,2):
            print(6%2)
            decToBin(3,2):
                print(3%2)
                decToBin(1,2):
                    print(1%2)
                    decToBin(0,2):
                        print(0)

New output:

decToBin(25, 2):
    print(25%2)
    print(12%2)
    print(6%2)
    print(3%2)
    print(1%2)
    print(0)

Upvotes: 3

Related Questions