R: Backwards for loop

1
2
3
for (i in 10:1) {
	print(i)
}

As easy as that.

Tags:

3 comments

  1. Input
    T<-10
    for (t in (T-1:1)){
    print("yes")
    for (s in (1:10)){
    print(s)
    }
    }

    Output:
    yes
    1 to 10 printed…
    Thus, outer loop is executed only once. I don't know what Im missing.

    • If you haven’t figured it out yet, it is because the brackets surround the colon operator in the first for loop i.e. (T-1:1).

      Assuming the brackets are there to denote precedence, the first for loop should be:

      for(t in (T-1):1){
      print(“yes”)

      }

      That should print 9 “yes”. Hope this helps.

Leave a comment