# 10-2 Exercise 1 - Selecting and writing appropriate loops

# Print the numbers 5 to 23

#1 Using a FOR loop - no need to initialise anything

for (i in 5:23) print(i)

#2 Using a WHILE loop without a break

i<-5 # Initialise loop variable

while(i<=23)
{
  print(i)
  i<-i+1 # Increment counter
}

# Print the squares of the numbers 18, 21, 49, 63

#3 Using a FOR loop over a vector

x<-c(18, 21, 49, 63) # Initialiase vector (NOT loop variable)

for (i in x) print(i*i)

# Print the squares of the numbers 5 to 23 
# until you come to a number greater than 400

#4 Using a WHILE loop with a break

i<-5 # Initialise loop variable

while (i<=23) 
{
  if(i^2>400) break
  print(i)
  i<-i+1
}

#5 Using a WHILE(TRUE) loop with a break

i<-5 # Initialise loop variable

while (TRUE) 
{
  if(i^2>400) break
  print(i)
  i<-i+1
}

#6 Using a REPEAT loop 

i<-5 # Initialise loop variable

repeat{
  print(i)
  i<-i+1
  if(i^2>400) break
}