[R] While loop working with TRUE/FALSE?

Milan Bouchet-Valat nalimilan at club.fr
Wed Feb 1 17:09:50 CET 2012


Le mercredi 01 février 2012 à 07:55 -0800, Chris82 a écrit :
> Hi R users,
> 
> is there any possibilty that a while loop is working like that:
> 
> z <- c(0,1,2,3,4,5,6,7,8,9)
> r <- 7
> 
>    while(w == T) { 
>         for ( i in 1:10 ){
>                 w <- r == z[i]
>                 print(w)  
>         }
> }
What problem are you trying to solve? In R, loops should generally be
avoided, and you have much simpler syntax for most cases.

> The loop should stop if w == TRUE
Your while loop stops when w == FALSE here. Anyway, it won't run since w
isn't set the first time the condition is tested. And I really don't see
why you have two embedded loops here.


Cheers



More information about the R-help mailing list