title: R, How does while (TRUE) work?
body: <p>I have to write a function of the following method :</p>
  
  <p><em>Rejection method (uniform envelope)</em>: </p>
  
  <p>Suppose that fx is non-zero only on [a, b], and fx ≤ k.</p>
  
  <ol>
  <li><p>Generate X ∼ U(a, b) and Y ∼ U(0, k) independent of X (so P =
  (X, Y ) is uniformly distributed over the rectangle [a, b] × [0, k]).</p></li>
  <li><p>If Y &lt; fx(x) then return X, otherwise go back to step 1.</p>
  
  <pre><code>rejectionK &lt;- function(fx, a, b, K) {
      # simulates from the pdf fx using the rejection algorithm
      # assumes fx is 0 outside [a, b] and bounded by K
      # note that we exit the infinite loop using the return statement
  
      while (TRUE) {
        x &lt;- runif(1, a, b)
        y &lt;- runif(1, 0, K)
        if (y &lt; fx(x)) return(x)
     }
  }
  </code></pre></li>
  </ol>
  
  <p>I have not understood why is this <code>TRUE</code> in  <code>while (TRUE)</code> ?</p>
  
  <p>if (y &lt; fx(x)) is not true then the method suggests to repeat the loop again to generate the uniform number again. (y &lt; fx(x)) is not true=FALSE. So why will not the condition be <code>while (FALSE)</code>?</p>
  
  <p>Again in which basis will i get enter into the  while loop ? That is, i am accustomed with this </p>
  
  <pre><code>   a=5 
     while(a&lt;7){
        a=a+1
     }
  </code></pre>
  
  <p>here i define a before writing the condition (a&lt;7) .</p>
  
  <p>But in <code>while (TRUE)</code> , which statement is true ?</p>
  
  <p>Additionally:</p>
  
  <p>you can run the codes</p>
  
  <pre><code>  rejectionK &lt;- function(fx, a, b, K) {
          # simulates from the pdf fx using the rejection algorithm
          # assumes fx is 0 outside [a, b] and bounded by K
          # note that we exit the infinite loop using the return statement
  
          while (TRUE) {
            x &lt;- runif(1, a, b)
            y &lt;- runif(1, 0, K)
            cat("y=",y,"fx=",fx(x),"",y &lt; fx(x),"\n")
            if (y &lt; fx(x)) return(x)
         }
      }
  
    fx&lt;-function(x){
       # triangular density
       if ((0&lt;x) &amp;&amp; (x&lt;1)) {
         return(x)
       } else if ((1&lt;x) &amp;&amp; (x&lt;2)) {
         return(2-x)
       } else {
         return(0)
       }
   }
  
   set.seed(123)
   rejectionK(fx, 0, 2, 1)
  </code></pre>