-3

I'm a beginner about R. How can I do a loop using "while", in order to get a sum of all natural numbers until this sum gets 1,000,000. When this sum exceed 1,000,000 , it has to stop (break). How can I do that?

JETR
  • 23
  • 2
  • 2
    there are plenty of documentation and examples in the `?'while'` help page and [here](http://stackoverflow.com/search?q=%5Br%5D+while) – rawr Aug 04 '15 at 03:10

1 Answers1

0

This is the basic way of doing a while break loop:

j <-  0

while(TRUE){
  j <- j+1
if(j>1000000)
break
}

to sum consecutive whole numbers in this loop:

j <-  0
while(TRUE){
  j <- j + (j+1)
if(j>1000000)
break
}
jalapic
  • 13,792
  • 8
  • 57
  • 87