# R – Loops

A loop is a method of repeating the same action over and over. We will separate loops into 2 types: For loops, and While loops.

For Loops are used to iterate between bounds. We declare them with an iterator variable, similar to mathematical notation (sums, products, etc.)

```for(i in 1:20){print(i)} # count from 1 to 20
for(i in 20:1){print(i)} # count from 20 to 1
for(i in 10:20){print(i)} # start at 10, count to 20
```

In fact, we can set the bounds of the for loop to be whatever we want, going forward, backwards, or even calling variables (So long as the call returns an integer).

```a = seq(1,32, by=1)     # sequence from 1 to 32
b = rep(NA, length(a))  # vector of missing values, length same as a
for(i in 1:length(a)){   # for i in 1 to 32
b[i] = 2^(a[i])        # b[i] = 2 to the a[i]
}
```

On an unrelated note, b[i] is the number of unique bitstrings that can be created using i bits.

The good news is that loops are simple and easy to understand in R. the bad news, is that loops in R are slow. There are various good practices we can do to keep them running as fast as possible, but ultimately loops are really slow. In practice, in places where the slowness of loops is an issue, we have other ways of doing the same thing.

```b = 2^a
```

This line of code will accomplish the same thing as the previous loop. Because a is a vector, each element of a will go through the mathematical function 2^a[i].

In addition, there are various methods that can be applied to complex data structures. Methods like lapply(), sapply(), mapply() allow us to do some things we would have done in a loop much faster.

While Loops, rather than using set boundaries like a for loop, use a condition to implicitly set the bounds of the loop.

```x=0
while(x < 40){  # While x is less than 40
x = x+1       # iterate x
print(x)
}
```

Note that at the 40th step, the value of x was less than 40 (39). Thus, the interpreter entered the loop again, and the final outout is 40. The condition statement in the parenthesis is a boolean statement. It has only two possible values, TRUE or FALSE. You can also nest boolean statements to allow for more complicated conditions.

Because loops are relatively slow, we try to avoid them in practice. Some tasks will ultimately call for a loop, so the functionality is there.