R, How does while (TRUE) work?

44,911

Solution 1

It's an infinite loop. The expression is executed as long as the condition evaluates to TRUE, which it will always do. However, in the expression there is a return, which when called (e.g., if y < fx(x)), breaks out of the function and thus stops the loop.

Here is a simpler example:

fun <- function(n) {
  i <- 1
  while (TRUE) {
    if (i>n) return("stopped") else print(i)
    i <- i+1
  }
}

fun(3)
#[1] 1
#[1] 2
#[1] 3
#[1] "stopped"

What happens when this function is called?

  1. i is set to 1.
  2. The condition of the while loop is tested. Because it is TRUE, it's expression is evaluated.
  3. The condition of the if construct is tested. Since it is FALSE the else expression is evaluated and i is printed.
  4. i is increased by 1.
  5. Steps 3 and 4 are repeated.
  6. When i reaches the value of 4, the condition of the if construct is TRUE and return("stopped") is evaluated. This stops the whole function and returns the value "stopped".

Solution 2

Inside while loop, if we have return statement with true or false.. it will work accordingly..

Example: To Check a list is circular or not..

here the loop is infinite, because while (true) is true always, but we can break sometimes by using the return statement,.

while(true)
{
if(!faster || !faster->next)
return false;
else
if(faster==slower || faster->next=slower)
{
printf("the List is Circular\n");
break;
}
else
{
slower = slower->next;
faster = faster->next->next;
}
Share:
44,911
user 31466
Author by

user 31466

Updated on July 09, 2022

Comments

  • user 31466
    user 31466 almost 2 years

    I have to write a function of the following method :

    Rejection method (uniform envelope):

    Suppose that fx is non-zero only on [a, b], and fx ≤ k.

    1. 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]).

    2. If Y < fx(x) then return X, otherwise go back to step 1.

      rejectionK <- 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 <- runif(1, a, b)
            y <- runif(1, 0, K)
            if (y < fx(x)) return(x)
         }
      }
      

    I have not understood why is this TRUE in while (TRUE) ?

    if (y < fx(x)) is not true then the method suggests to repeat the loop again to generate the uniform number again. (y < fx(x)) is not true=FALSE. So why will not the condition be while (FALSE)?

    Again in which basis will i get enter into the while loop ? That is, i am accustomed with this

       a=5 
       while(a<7){
          a=a+1
       }
    

    here i define a before writing the condition (a<7) .

    But in while (TRUE) , which statement is true ?

    Additionally:

    you can run the codes

      rejectionK <- 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 <- runif(1, a, b)
              y <- runif(1, 0, K)
              cat("y=",y,"fx=",fx(x),"",y < fx(x),"\n")
              if (y < fx(x)) return(x)
           }
        }
    
      fx<-function(x){
         # triangular density
         if ((0<x) && (x<1)) {
           return(x)
         } else if ((1<x) && (x<2)) {
           return(2-x)
         } else {
           return(0)
         }
     }
    
     set.seed(123)
     rejectionK(fx, 0, 2, 1)
    
  • user 31466
    user 31466 over 10 years
    i apologize. I have not understood yet. Which statement need to be TRUE? i>n or i<n ?
  • Roland
    Roland over 10 years
    Sorry, I don't understand your question.
  • user 31466
    user 31466 over 10 years
    when i=1 why will i enter the loop ? 1 is not greater than 3 so this is FALSE and i need to repeat the loop until it returns "stopped". hence Why is not the condition while(FALSE) so that i can get enter the loop.
  • blmoore
    blmoore over 10 years
    You're missing the point, think of while(TRUE) as while(TRUE==TRUE), i.e. continue regardless
  • Kevin Ushey
    Kevin Ushey over 10 years
    FYI, R actually has the keyword repeat which is perhaps a more clear version of while(TRUE) (although honestly it seems very rarely used)
  • caner
    caner about 5 years
    for mentioning the use of break