Recursion in for loops

So I got curious about how recursion in for loops works.

Here's the snippet in JS:

const run = (recur, n, i) => {
  console.log(`---recur: ${recur}, n: ${n}, i: ${i}---`);
  if (n === recur) {
    console.log(`*return* i:${i}, n:${n}`);
  for(i = 0; i < 2; i++) {
    console.log(`i:${i}, n:${n}`);
    run(recur, n + 1, i);

run(2, 0);

JS Bin on

After studying the output for a long long time...this is what the conclusion I came up with, regarding the flow of the function.


This is purely my own deduction.

Recursion in for loops
Share this