<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mohd Abdul Naveed</title>
    <description>The latest articles on DEV Community by Mohd Abdul Naveed (@mohd_abdulnaveed_bea3d39).</description>
    <link>https://dev.to/mohd_abdulnaveed_bea3d39</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3745008%2Fb4d481b1-01f0-4897-928d-3bb75fb78285.png</url>
      <title>DEV Community: Mohd Abdul Naveed</title>
      <link>https://dev.to/mohd_abdulnaveed_bea3d39</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mohd_abdulnaveed_bea3d39"/>
    <language>en</language>
    <item>
      <title>How to Know When a for Loop Belongs Inside Recursion</title>
      <dc:creator>Mohd Abdul Naveed</dc:creator>
      <pubDate>Sun, 01 Feb 2026 10:02:23 +0000</pubDate>
      <link>https://dev.to/mohd_abdulnaveed_bea3d39/how-to-know-when-a-for-loop-belongs-inside-recursion-3dcf</link>
      <guid>https://dev.to/mohd_abdulnaveed_bea3d39/how-to-know-when-a-for-loop-belongs-inside-recursion-3dcf</guid>
      <description>&lt;p&gt;Many developers struggle with knowing when a for loop belongs inside a recursive function. The confusion isn’t about syntax, it’s about understanding what recursion is responsible for, and what it isn’t.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why this matters in real code
&lt;/h2&gt;

&lt;p&gt;I ran into this issue while solving &lt;strong&gt;backtracking problems&lt;/strong&gt; and building &lt;strong&gt;traversal logic&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;My recursive functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;compiled ✔️&lt;/li&gt;
&lt;li&gt;ran ✔️&lt;/li&gt;
&lt;li&gt;looked correct ✔️&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;…but the results were &lt;strong&gt;incomplete or duplicated&lt;/strong&gt;.&lt;br&gt;
The bug wasn’t the recursion itself.&lt;/p&gt;

&lt;p&gt;It was a misunderstanding &lt;strong&gt;when multiple choices must be explored at the same level&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhoxc06vrz3djjmic8i2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhoxc06vrz3djjmic8i2.png" alt="Diagram showing recursive calls stacking downward like floors in a building, illustrating how recursion moves deeper with each function call" width="674" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A recursive function has &lt;strong&gt;two essential parts&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Base case&lt;/strong&gt; — when to stop&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Recursive call&lt;/strong&gt; — how to move closer to that stop&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each recursive call:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;creates a new stack frame&lt;/li&gt;
&lt;li&gt;represents &lt;strong&gt;one level deeper&lt;/strong&gt; in the problem
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Call Stack (top)
┌─────────────┐
│ f(1)        │ ← last in
│ f(2)        │
│ f(3)        │
└─────────────┘
Call Stack (bottom)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Stacks are &lt;strong&gt;LIFO&lt;/strong&gt; — last in, first out.&lt;/p&gt;
&lt;h2&gt;
  
  
  Problems with no branching (no loops)
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Story: going down the stairs
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Facywdwz54a1kbhgncide.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Facywdwz54a1kbhgncide.png" alt="Illustration of a person going down floors using a single door at each level, representing recursion with only one possible path and no branching" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Imagine a building with many floors.&lt;br&gt;
Each recursive call moves you &lt;strong&gt;down one floor&lt;/strong&gt;.&lt;br&gt;
You stop at the &lt;strong&gt;ground floor&lt;/strong&gt; — the base case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int go_down(int floor) {
    if (floor == 0) return;
    go_down(floor - 1);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Floor 3
  ↓
Floor 2
  ↓
Floor 1
  ↓
Floor 0  (base case)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;only &lt;strong&gt;one path&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;no choices&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 &lt;strong&gt;No &lt;code&gt;for&lt;/code&gt; loop belongs here&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  When loops become necessary
&lt;/h2&gt;

&lt;p&gt;Now imagine &lt;strong&gt;each floor has multiple doors&lt;/strong&gt; 🚪🚪🚪&lt;br&gt;
Each door leads to the &lt;strong&gt;next floor&lt;/strong&gt;.&lt;br&gt;
That means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Floors&lt;/strong&gt; → recursion&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Doors&lt;/strong&gt; → loop&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Visualizing the idea&lt;/strong&gt;
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Floor 3
 ├─ Door A → Floor 2
 ├─ Door B → Floor 2
 └─ Door C → Floor 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choose a door (loop)&lt;/li&gt;
&lt;li&gt;Go down one floor (recursion)&lt;/li&gt;
&lt;li&gt;Repeat&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Code example: visiting rooms
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void visit_rooms(int floor) {
    if (floor == 0) {
        cout &amp;lt;&amp;lt; "Ground floor reached" &amp;lt;&amp;lt; endl;
        return;
    }

    char doors[] = {'A', 'B', 'C'};
    for (char door : doors) {
        cout &amp;lt;&amp;lt; "Using door " &amp;lt;&amp;lt; door &amp;lt;&amp;lt; " on floor " &amp;lt;&amp;lt; floor &amp;lt;&amp;lt; endl;
        visit_rooms(floor - 1);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Why the loop exists
&lt;/h3&gt;

&lt;p&gt;At the &lt;strong&gt;same level&lt;/strong&gt;, you must try:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Door A&lt;/li&gt;
&lt;li&gt;Door B&lt;/li&gt;
&lt;li&gt;Door C
Same level → loop
Next level → recursion&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  When a loop does NOT belong
&lt;/h2&gt;

&lt;p&gt;If there is &lt;strong&gt;only one action&lt;/strong&gt;, looping is incorrect.&lt;/p&gt;
&lt;h3&gt;
  
  
  Countdown example
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void countdown(int n) {
    if (n == 0) return;
    cout &amp;lt;&amp;lt; n &amp;lt;&amp;lt; endl;
    countdown(n - 1);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;OutPut:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3 → 2 → 1 → 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No choice&lt;/li&gt;
&lt;li&gt;No doors&lt;/li&gt;
&lt;li&gt;No flavors&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Golden rule (remember this forever)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Recursion goes down&lt;br&gt;
&lt;strong&gt;Loops go across&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If these roles blur, your mental model breaks down.&lt;/p&gt;

&lt;h2&gt;
  
  
  A quick self-check
&lt;/h2&gt;

&lt;p&gt;Ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“At this step, do I need to try multiple options?”&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;❌ No → no loop&lt;/li&gt;
&lt;li&gt;✅ Yes → loop required&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you can’t name the options, the loop doesn’t belong.&lt;/p&gt;

&lt;h2&gt;
  
  
  A very common mistake
&lt;/h2&gt;

&lt;p&gt;You:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;have a base case&lt;/li&gt;
&lt;li&gt;call the function recursively&lt;/li&gt;
&lt;li&gt;get output&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;…but it’s &lt;strong&gt;missing results&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This happens when recursion is expected to handle &lt;strong&gt;branching&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It won’t.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example: binary strings (classic pitfall)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Goal:&lt;/strong&gt; generate all binary strings of length &lt;code&gt;n&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Expected output for &lt;code&gt;n = 2&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;00
01
10
11
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;❌ Incorrect approach&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void generate(int n, string s) {
    if (n == 0) {
        cout &amp;lt;&amp;lt; s &amp;lt;&amp;lt; endl;
        return;
    }

    generate(n - 1, s + "0");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What actually happens&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;generate(2, "")
  → generate(1, "0")
    → generate(0, "00")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only one path is explored.&lt;/p&gt;

&lt;h2&gt;
  
  
  What was missing?
&lt;/h2&gt;

&lt;p&gt;At each level, there are &lt;strong&gt;two choices&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"" 
 ├─ "0"
 └─ "1"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 Recursion went down, but never sideways.&lt;/p&gt;

&lt;h2&gt;
  
  
  Think in terms of STATE (this is the mental unlock)
&lt;/h2&gt;

&lt;p&gt;A recursive function always represents a &lt;strong&gt;state&lt;/strong&gt;.&lt;br&gt;
A state answers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“Where am I right now?”&lt;/li&gt;
&lt;li&gt;“What has already been decided?”&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Examples of state:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;current index&lt;/li&gt;
&lt;li&gt;current depth&lt;/li&gt;
&lt;li&gt;remaining steps&lt;/li&gt;
&lt;li&gt;current path&lt;/li&gt;
&lt;li&gt;current floor&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;generate(2, "")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;State:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;depth = 2&lt;/li&gt;
&lt;li&gt;string = ""&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Next level (same depth, different choices):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;generate(1, "0");
generate(1, "1");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Diagram:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Level 0: ""
 ├─ Level 1: "0"
 │   ├─ "00"
 │   └─ "01"
 └─ Level 1: "1"
     ├─ "10"
     └─ "11"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The rule (clearly stated)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Recursion moves between states&lt;br&gt;
&lt;strong&gt;Loops explore multiple actions from the same state&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Skip the loop → lose possibilities.&lt;/p&gt;

&lt;h2&gt;
  
  
  Classic example: permutations
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Input
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1, 2, 3]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1,2,3]
[1,3,2]
[2,1,3]
[2,3,1]
[3,1,2]
[3,2,1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step-by-step thinking
&lt;/h2&gt;

&lt;p&gt;You build the permutation &lt;strong&gt;one position at a time&lt;/strong&gt;.&lt;br&gt;
At each step:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;choose one unused number&lt;/li&gt;
&lt;li&gt;try &lt;strong&gt;every possible choice&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  State definition
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;path&lt;/code&gt; → current permutation&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;used[]&lt;/code&gt; → which numbers are taken
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void permute(vector&amp;lt;int&amp;gt;&amp;amp; nums, vector&amp;lt;int&amp;gt;&amp;amp; path, vector&amp;lt;bool&amp;gt;&amp;amp; used) {
    if (path.size() == nums.size()) {
        print(path);
        return;
    }

    for (int i = 0; i &amp;lt; nums.size(); i++) {
        if (used[i]) continue;

        used[i] = true;
        path.push_back(nums[i]);

        permute(nums, path, used);

        path.pop_back();
        used[i] = false;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Diagram&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[]
 ├─ [1]
 │   ├─ [1,2]
 │   │   └─ [1,2,3]
 │   └─ [1,3]
 │       └─ [1,3,2]
 ├─ [2]
 │   ├─ [2,1]
 │   └─ [2,3]
 └─ [3]
     ├─ [3,1]
     └─ [3,2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why recursion alone isn’t enough&lt;/strong&gt;&lt;br&gt;
This line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;permute(nums, path, used)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;does &lt;strong&gt;not&lt;/strong&gt; automatically try other numbers.&lt;br&gt;
Recursion:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;moves deeper
Loops:&lt;/li&gt;
&lt;li&gt;create branches
They solve &lt;strong&gt;different problems&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The real golden rule
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Recursion answers: “What is the next state?”&lt;br&gt;
Loops answer: &lt;strong&gt;“What are my options right now?”&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If recursion is expected to replace branching, the algorithm will silently miss valid results.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where you’ll see this pattern again
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;DFS / BFS&lt;/li&gt;
&lt;li&gt;Backtracking problems&lt;/li&gt;
&lt;li&gt;Tree traversals&lt;/li&gt;
&lt;li&gt;Graph search&lt;/li&gt;
&lt;li&gt;Combinatorics (subsets, permutations, combinations)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once you separate state transitions from choice exploration, recursion problems become easier to reason about and debug.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>programming</category>
      <category>recursion</category>
      <category>computerscience</category>
    </item>
  </channel>
</rss>
