What is the correct syntax for a for loop in R?

What is the correct syntax for a for loop in R?

a) for(i = 1; i <= 5; i++)
b) for(i in 1:5)
c) for i in 1:5
d) for(i, 1:5)

Answer:

b) for(i in 1:5)

Explanation:

The correct syntax for a for loop in R is for(i in 1:5), where i takes values from 1 to 5 in each iteration.

Reference:

R Programming – Loops MCQ Questions and Answers

Scroll to Top