Skip to content
Prev 43943 / 398525 Next

Printting 'for' and 'while' indices

Consider the following: 

 > i <- 0
 > while(i < 5) {
    "do stuff"
    print(i)
    i <- i + 1
}
[1] 0
[1] 1
[1] 2
[1] 3
[1] 4
 > i <- 0
 > while(i < 5) {
    "do stuff"
    cat(i, "")
    i <- i + 1
}
0 1 2 3 4 > i <- 0
 > while(i < 100) {
    "do stuff"
    if((i %% 10) == 0)
        cat(i, "")
    i <- i + 1
}
0 10 20 30 40 50 60 70 80 90

      This was produced in S-Plus 6.2 under Windows 2000.  For some 
reason, when I copy this code from S-Plus to R1.8.1 via XEmacs, this is 
locking up R for me right now, and I can't figure out why.  However, I 
see no reason why these constructs would not work in R. 

      hope this helps. 
      spencer graves
Marcos Sanches wrote: