for, while, repeat loop Questions (basic)

Hi, I am a newbie in R Programming.

Studying for, while, and repeat loop coding.

for(i in 5:1) print(rep(i*2,i))

[1] 10 10 10 10 10

[1] 8 8 8 8

[1] 6 6 6

[1] 4 4

[1] 2

I completed for assignment however, I couldn't quite understand while and repeat function..

[while]

i<-10
while(i<=2){
+print(rep(i, i))
+i<-i-2
+}

[repeat]

i<-10

repeat{

  • if(i<2)break

  • print(rep(i,i))

  • i<-i-2

  • }

[1] 10 10 10 10 10 10 10 10 10 10

[1] 8 8 8 8 8 8 8 8

[1] 6 6 6 6 6 6

[1] 4 4 4 4

[1] 2 2

Can anyone see what is wrong with my code?

Thank you in advance!!

Hi ycollyer,

When you said "wrong," do you expect your While code to print something?
If that's the case, it's because you assign i as 10. While i <= 1, it would print, but since i is 10, then it didn't print.

If that's not the "wrong" you are thinking, please elaborate.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.