Check out example codes for "r for loop". It will help you in understanding the concepts better.

Code Example 1

# Basic syntax:
for (i in sequence) {
  code to be repeated
}

# Example usage:
for (i in 1:5) {
  print(i)
}
# Returns:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

# Note, you can completely exit from a loop with "break", or skip to the
#	next iteration of a loop with "next"

# Example of next and break:
for (i in 1:5) {
  if (i == 2) { # Skip to next iteration if i = 2
    next
    }
  if (i == 4) { # Exit loop entirely if i = 4
    break
    }
  print(i)
}
# Returns:
[1] 1
[1] 3

Code Example 2

for (val in sequence)
{
statement
}

Code Example 3

for (val in x) {
if(val %% 2 == 0)  count = count+1
}

Learn ReactJs, React Native from akashmittal.com