# For and while loops

## For loops

For loops are a central component of any programming language so it is useful to know how to use them in R however, the way that the R language is constructed means that there is a faster way of performing the same function as a for loop which we will look at in the Apply section.

In the example below we are going to use a for loop to iterate over a vector to perform a calculation. First we create the vector x on which the calculation will be performed. Then we create a vector of zeros using the `rep()` function into which we will put the output of the calculation. If you do not know how many instances are going to be output from your function you can use a list to store the outputs of the calculation.

The first line of the for loop can be described as: `for (counter in start:end)`. The variable i will take the values described following the word in. There are many ways to make a for loop step through various values which is something you can explore. Normally we just want to iterate over a range of numbers which most commonly represent row/element indices.

Within the curly braces we then provide a task to be performed on our data. In this instance we take the ith element of x and add the value of i to it. This value is then assigned to the ith element of y.

``````
Input:
x <- 1:10
x

Output:
1 2 3 4 5 6 7 8 9 10

Input:
y <- rep(0,length(x))
for (i in 1:length(x)){
y[i] <- x[i] + i
}
y

Output:
2 4 6 8 10 12 14 16 18 20
``````
``` ```

When you want to perform a calculation over an array you can embed one for loop inside another to iterate over the rows and columns of the array. We will look at the faster more R-ish way of doing this in the Apply section.

``````
Input:
nums <- sample(1:50,20,replace=T)
x <- matrix(nums,nrow=4,ncol=5)
x

Output:
8	32	30	43	36
46	23	6	3	3
20	34	41	10	31
49	26	3	40	42

Input:
out <- matrix(rep(0,20),nrow=4,ncol=5)
for (i in 1:nrow(x)){
for (j in 1:ncol(x)){
out[i,j] <- x[i,j] + i
}
}
out

Output:
9	33	31	44	37
48	25	8	5	5
23	37	44	13	34
53	30	7	44	46
``````
``` ```

## While loops

While loops are most useful when you do not know how many iterations over the data you are going to need to perform. A while loop will keep running until a certain condition is met. Be careful because if the condition is never met you will create an infinite loop!

In the example below we will perform the same calculation as we did using the for loop but we will have it stop when i reaches six. You will notice in this instance we have to say what number i is to start with by assigning a value to it before the while loop. The condition we are using is while i is less than 6, perform the calculation. You will also notice that we have to manually increment i after the calculation has been performed. If you don't do this you will again have an infinite loop.

``````
Input:
x <- 1:10
x

Output:
1 2 3 4 5 6 7 8 9 10

Input:
y <- rep(0,10)
i <- 1
while (i < 6){
y[i] <- x[i] + i
i <- i+1
}
print(y)

Output:
  2  4  6  8 10  0  0  0  0  0
``````
``` ```