OneBite.Dev - Coding blog in a bite size

implement a three nested loop in R

Code snippet on how to implement a three nested loop in R

  for (i in 1:3) {
    for (j in 1:3) {
      for (k in 1:3) {
        print(paste("i=",i," j=",j," k=",k))
      }
    }
  }

This code uses a three nested loop to print out values of “i,” “j,” and “k.” The outermost loop is the first “for” loop, which executes the code within it three times. This loop is responsible for changing the value of “i”. For every execution of the first “for” loop, the second “for” loop is executed. This loop is responsible for changing the value of “j”. Similarly, for every execution of the second loop, the third “for” loop is executed, which is responsible for changing the value of “k.” Inside the innermost loop, the code prints out a string formed by combining the values of “i,” “j,” and “k.” After printing out the string, the loop is exited and the control moves back up to the loop before it. This way, the code executes for all possible combinations of “i,” “j,” and “k.”

r