# Looping from a list

# For every element in the vector, this will add the
# current value of the loop list to it

x<-c(2,4,0,3,1)   # Set up vector to be looped through
y<-NULL           # Set up an empty vector to copy into

for (i in 1:5) {  # Loop from 1 to 5
  y<-c(y,x[i]+i)  # Add the current value+loop value to y
}

y   # Display the contents of y

# Looping with seq()

# This will copy elements from one vector to another
# until it reaches a 0 value, when it will stop

x<-c(2,4,0,3,1)   # Set up vector to be looped through
y<-NULL           # Set up an empty vector to copy into

for(i in seq(x)) {  # Loop through all elements of x
  y<-c(y,x[i])      # Add element x[i] to the end of y
  if (x[i]==0) break  # if x[i] is 0 then exit the loop
}

y   # display the contents of y
