title: Performing Recursion inside For loop
body: <p>I am trying to understand the code of writing permutations for a given input string.</p>
  
  <p>For Example: <strong>Input String:123,Output:123,132,213,231,312,321.</strong></p>
  
  <p>Below pasted  code snippet does that.</p>
  
  <pre><code> public static void main(String args[]) {
  
      permuteString("", "123");
     }
  
  public static void permuteString(String beginningString, String endingString) {
      if (endingString.length() &lt;= 1)
          System.out.println(beginningString + endingString);
      else
          for (int i = 0; i &lt; endingString.length(); i++) {
              try {
                   // System.out.println(i);
                  String newString = endingString.substring(0, i) + endingString.substring(i + 1);
                  permuteString(beginningString + endingString.charAt(i), newString);
              } catch (StringIndexOutOfBoundsException exception) {
                  exception.printStackTrace();
              }
          }
  </code></pre>
  
  <p>I am getting very confused about when  integer <code>'i'</code> gets incremented in the for loop i.e from <code>i=0 to 1.</code>
  one thing i understood from the first iteration is <code>'i'</code> got incremented to 1 when it hit the base case i.e</p>
  
  <pre><code>   if (endingString.length() &lt;= 1)
          System.out.println(beginningString + endingString);
  </code></pre>
  
  <p>I tried to debug further , i value kept on changing between 0 and 1 for successive iterations which i couldn't understand.</p>
  
  <p>To Sum-up,
     I am confused regarding the relationship  between  <code>"for loop"</code> and the two instructions in the try block after the first iteration.</p>
  
  <p>I would be happy if some one can guide me through the process.</p>