Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>while</code> body is executed only once in each recursive depth. The <code>while</code> is false when <code>i</code> is <code>10</code>, which will be when <code>tester ()</code> is called 10 times recursively. As the <code>i</code> is declared global, the <code>i++</code> update will be visible to each call of <code>tester ()</code>.</p> <p>At the last recursive depth when the <code>while</code> condition is false, the last <code>tester ()</code> call will return to its previous depth. At this moment, the next iteration of the <code>while</code> loop will be false as <code>i</code> is <code>10</code>. The <code>cout</code> statement will be encountered after each <code>while</code> loop is terminated, which will sequentially print the values of <code>k</code>, incrementing at each recursive depth, as the recursion rolls back.</p> <p>Manually trace what happens to understand the stuff.</p> <p><strong>UPDATE</strong></p> <p>Have a look at the execution output. Especially note the <code>d</code> parameter, which denotes the recursive depth. At each depth the <code>while</code> loop has iterated one time, upto the last <em>"while is true, tester called"</em> output. At depth 10, the <code>while</code> is false as it is 10, and first time it returns back the control to its previous depth level, (the first "return back" when <code>i</code> is <code>10</code>). After it returns the control returns to the body of the <code>while</code> loop of the last level, from which the function was called (from which it just returned), and the next iteration of this loop will be false, (i is global and 10 or more), therefore this also returns. Similarly, at each recursive depth the 2nd iteration of <code>while</code> loop is false, and it keeps returning. Check out the output. </p> <pre> i k d 1 1 0, call from main 2 1 1, while is true, tester called 3 1 2, while is true, tester called 4 1 3, while is true, tester called 5 1 4, while is true, tester called 6 1 5, while is true, tester called 7 1 6, while is true, tester called 8 1 7, while is true, tester called 9 1 8, while is true, tester called 10 1 9, while is true, tester called 11 1 10, returning back, print k // this step the while is false in depth 10 12 2 9, returning back, print k // from now on, as the recursion rolls back 13 3 8, returning back, print k // the second iteration at each recursive 14 4 7, returning back, print k // depth will be executed, and each while 15 5 6, returning back, print k // condition will be false, therefore it will 16 6 5, returning back, print k // not call tester anymore and return the control 17 7 4, returning back, print k // to the previous level. NOICE the `d' parameter 18 8 3, returning back, print k 19 9 2, returning back, print k 20 10 1, returning back, print k </pre> <p>And here is the test code. I hope the description is much clear now (?). Analyzing this will help.</p> <pre><code>#include &lt;stdio.h&gt; int i = 1; int k = 1; void tester (int d) { while (i++ &lt; 10) { printf ("%2d %2d %2d, while is true, tester called\n", i, k, d); tester (d+1); } printf ("%2d %2d %2d, returning back, print k\n", i, k++, d); } int main (void) { int depth = 0; printf ("i k d\n"); printf ("%2d %2d %2d, call from main\n", i, k, depth); tester (depth + 1); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload