# 10-1 Six Basic Loops

# The for loop (version 1) - no break needed

for (i in 1:10) print(i) # no need to intitialise anything



# The for loop (version 2) - no break needed

x<-c(2,13,25,42,57,82) # initialise vector to loop through

for (i in x) print(i) # no need to initialise loop counter



# The for loop (version 3) - optional break

x<-c(2,13,25,42,57,82) # initialise vector to loop through

for (i in x) { # no need to initialise loop counter
  if (i>40) break # break in the 4th cycle as i is then 42
  print(i) # print i
}



# The while loop (version 1) - no break needed

i<-1 # initialise loop counter

while (i<=10) {  # stops when i reaches 11
  print(i) # print i
  i<-i+1   # increment i
}
  



# The while loop (version 2) - break needed

i<-1 # initialise loop counter

while(TRUE) { # start and keep going until a break
  print(i) # print i
  i<-i+1   # increment i
  if (i>10) break # break when i reaches 11
}



# The repeat loop - no exit condition - break needed

i<-1 # initialise loop counter

repeat { # start and keep going until a break
  print(i) # print i
  i<-i+1   # increment i
  if (i>10) break # break when i reaches 11
}