title: Return object from while loop
body: <p>UPDATE see below</p>
  
  <p>I am not sure how to populate a list or vector with elements in a while loop when there is no counter/iterator to index by. See below minimal example. I have a more complicated while loop that, like foo below, has a logical statement that doesn't include a number, so it's not clear to me how to populate the list vec with x. Is it possible to create some kind of iterator within the while loop so that each time you go through the while loop the iterator simply increases by one? I could then use this iterator to index the list vec?</p>
  
  <pre><code>foo &lt;- function(){
    names &lt;- list("a","b","c")
    vec &lt;- list()
    more &lt;- TRUE
    while(more == TRUE){
     names &lt;- rep(names, 2)
     if(length(names) &lt; 50) vec &lt;- names
     more &lt;- length(names) &lt; 50
     }
    vec
  }
  
  foo()
  </code></pre>
  
  <p>Thanks, this seems to work as I wanted now:</p>
  
  <pre><code>foo &lt;- function(){
    names &lt;- list("a","b","c")
    i &lt;- 0
    vec &lt;- list()
    more &lt;- TRUE
    while(more == TRUE){
      i &lt;- i + 1
      names &lt;- rep(names, 2)
      if(length(names) &lt; 50) vec[[i]] &lt;- names
      more &lt;- length(names) &lt; 50
    }
   vec
  }
  
  foo()
  </code></pre>