```{r, include = FALSE}
knitr::opts_chunk$set(fig.width=4, fig.height=4, fig.path = "fig/loops-")
```

# For loops

We are not covering much about the programming side of R today. However `for` loops are useful even for interactive work.

If you intend to take your knowledge of R further, you should also investigate writing your own `function`s, and `if` statements.

For loops are the way we tell a computer to perform a repetitive task. Under the hood, many of the functions we have been using today use for loops.

If we can't find a ready made function to do what we want, we may need to write our own for loop.

## Preliminary: blocks of code

Suppose we want to print each word in a sentence, and for some reason we want to do this all at once. One way is to use six calls to `print`:

```{r}
sentence <- c("Let", "the", "computer", "do", "the", "work")

{
  print(sentence[1])
  print(sentence[2])
  print(sentence[3])
  print(sentence[4])
  print(sentence[5])
  print(sentence[6])
}
```

R treats the code between the `{` and the `}` as a single "block". It reads it in as a single unit, and then executes each line in turn with no further interaction.

## For loops

What we did above was quite repetitive. It's always better when the computer does repetitive work for us.

Here's a better approach, using a for loop:

```{r}
for (word in sentence) {
    print(word)
}
```

The general form of a loop is:

```{r, eval=FALSE}
for (variable in collection) {
  do things with variable
}
```

We can name the loop variable anything we like (with a few restrictions, e.g. the name of the variable cannot start with a digit). `in` is part of the `for` syntax. Note that the body of the loop is enclosed in curly braces `{ }`. For a single-line loop body, as here, the braces aren't needed, but it is good practice to include them as we did.


## Accumulating a result

Here's another loop that repeatedly updates a variable:

```{r}
len <- 0
vowels <- c("a", "e", "i", "o", "u")
for (v in vowels) {
  len <- len + 1
}
# Number of vowels
len
```


It's worth tracing the execution of this little program step by step.
Since there are five elements in the vector `vowels`, the statement inside the loop will be executed five times.
The first time around, `len` is zero (the value assigned to it on line 1) and `v` is `"a"`.
The statement adds 1 to the old value of `len`, producing 1, and updates `len` to refer to that new value.
The next time around, `v` is `"e"` and `len` is 1, so `len` is updated to be 2.
After three more updates, `len` is 5; since there is nothing left in the vector `vowels` for R to process, the loop finishes.

Note that a loop variable is just a variable that's being used to record progress in a loop.
It still exists after the loop is over, and we can re-use variables previously defined as loop variables as well:

```{r}
letter <- "z"
for (letter in c("a", "b", "c")) {
  print(letter)
}
# after the loop, letter is
letter
```


> ### Challenge - Using loops {.challenge}
>
> 1. Recall that we can use `:` to create a sequence of numbers.
>
> ```{r}
> 1:5
> ```
>
> Suppose the variable `n` has been set with some value, and we want to print out the numbers up to that value, one per line.
>
> Write a for loop to achieve this.
>
>
> 2. Suppose we have a vector called `vec` and we want to find the total of all the numbers in `vec`.
>
> Write a for loop to calculate this total.
>
> (R has a built-in function called `sum` that does this for you.
> Please don't use it for this exercise.)
>
>
> 3. Exponentiation is built into R:
>
> ```{r}
> 2^4
> ```
>
> Suppose variables `base` and `power` have been set.
>
> Write a for loop to raise `base` to the power `power`.
>
> Try it with various different values in `base` and `power`.
>


Many of the functions and operators we have been using are implemented using for loops, so often in R we are able to use these rather than directly writing a for loop. However when we need to do something complicated, for loops are there for us. Some real world reasons you might use a for loop:

* To create a collection of similar plots.

* To load and process a collection of files, all in the same way.

* To perform a Monte Carlo simulation to estimate the power of a proposed experiment, for a given effect size and expected noise due to measurement error and biological variation.

* To perform resampling such as a permutation test or a bootstrap, to assure yourself that some result you have calculated is not simply due to chance.
