R, How does while (TRUE) work?
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?
i
is set to 1.- The condition of the
while
loop is tested. Because it isTRUE
, it's expression is evaluated. - The condition of the
if
construct is tested. Since it isFALSE
theelse
expression is evaluated andi
is printed. i
is increased by 1.- Steps 3 and 4 are repeated.
- When
i
reaches the value of 4, the condition of theif
construct isTRUE
andreturn("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;
}
user 31466
Updated on July 09, 2022Comments
-
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.
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]).
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
inwhile (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 over 10 yearsi apologize. I have not understood yet. Which statement need to be TRUE? i>n or i<n ?
-
Roland over 10 yearsSorry, I don't understand your question.
-
user 31466 over 10 yearswhen 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 over 10 yearsYou're missing the point, think of
while(TRUE)
aswhile(TRUE==TRUE)
, i.e. continue regardless -
Kevin Ushey over 10 yearsFYI, R actually has the keyword
repeat
which is perhaps a more clear version ofwhile(TRUE)
(although honestly it seems very rarely used) -
caner about 5 yearsfor mentioning the use of
break