title: Do while loops store intermediate results?
body: <p>I have the following loop that keeps generating 3 random numbers until these 3 random numbers sum to exactly 10. Then, it will repeat this process 100 times. Here is the code for this:</p>
  <pre><code>for (i in 1:100){
      num_1_i = num_2_i = num_3_i = 0
      
      while(num_1_i + num_2_i + num_3_i != 10){
          num_1_i = runif(1,0,5)
          num_2_i = runif(1,0,5)
          num_3_i = runif(1,0,5)
      }
      
      inter_results_i &lt;- data.frame(i, num_1_i, num_2_i, num_3_i)
      list_results[[i]] &lt;- inter_results_i
  }
  </code></pre>
  <p>I know that this WHILE LOOP will take a very long time to run given that the WHILE condition is very &quot;strict&quot; (i.e. its pretty difficult to come up with 3 random numbers that sum to a specific number). Suppose I were to run this WHILE LOOP for a few hours and notice that the code is still running - if I were to &quot;interrupt&quot; the computer (e.g. click the &quot;interrupt button&quot; in R, i.e. red stop sign in the corner), I would likely loose all the &quot;progress&quot; I had made (e.g. suppose the computer generated 5 triples of numbers that summed to 10).</p>
  <p><strong>My Question:</strong> Can this while loop be altered in such a way, such that upon interruption, the &quot;intermediate progress&quot; is saved in the &quot;list_results&quot; object? <strong>I am interested in learning how to modify the general code for a WHILE LOOP such that upon interruption, the intermediate progress is saved.</strong></p>
  <p><strong>Note:</strong> I would be interested to see an example of how to write <em>any</em> WHILE LOOP that is capable of storing intermediate results.</p>