## for Loops

• A for loop repeats a chunk of code many times, once for each element in a set of input.
for (value in that) {
this
}
• The that object should be a set of objects (often a vector of numbers or character strings).
• 1:10
• 1:length(x)
• 1:nrow(x)
• c("hello", "this", "is", "a", "vector", "of", "strings")

## for Loops

• The value symbol in a for loop acts like an argument in a function.
• The for loop will
• create an object named value
• and assign it a new value on each run of the loop.
• The code in your loop can access this value by calling the value object.

## for Loops

for (value in c("My", "first", "for", "loop")) {
print("one run")
}
## [1] "one run"
## [1] "one run"
## [1] "one run"
## [1] "one run"

## for Loops

for (value in c("My", "second", "for", "loop")) {
print(value)
}
## [1] "My"
## [1] "second"
## [1] "for"
## [1] "loop"

## for Loops

• Example: What will be the value of a after the loop?
a <- 100

for (a in 1:5) {
print(a)
}

a

## Exercise 1

• Task: Write a for loop to compute the sum of 5 numbers that are randomly selected from 1 to 100 with replacement.

## Exercise 1

• Task: Write a for loop to compute the sum of 5 numbers that are randomly selected from 1 to 100 with replacement.

• First, let’s generate the 5 random numbers!

• Second, write the for loop.

## Working with for loop

• To save output from a for loop, you must write the loop so that it saves its own output as it runs.
• What that means:
• Create an empty vector or list before you run the loop.
• Use the for loop to fill up the vector or list.
• When the for loop is finished, you’ll be able to access the vector or list, which will now have all of your results.

## Exercise 2

• From a given list of fruits, select only the ones that have 6 or fewer letters.
fruits <- c("apple", "pineapple", "watermelon", "orange", "peach", "plum",
"honeydew", "banana", "kiwi", "papaya", "grapes", "strawberry",
"blueberry", "blackberry")

## while Loops

• A while loop reruns a chunk while a certain condition remains TRUE.
while (condition) {
code
}
• while will rerun condition (a logical test) at the start of each loop:
• If condition evaluates to TRUE, while will run the code between its braces.
• If condition evaluates to FALSE, while will finish the loop.

## while Loops

• Why might condition change from TRUE to FALSE?
• The code inside the loop changes the certain values which change the result of condition. (We will look at an example next)
• If the code has no relationship to the condition, a while loop will run until you stop it.
• Called infinite loop.

## while Loops

cash <- 20
n <- 0

while (cash > 0) {
cash <- cash - 1
n <- n + 1
}

n
## [1] 20

## Exercise 3

• Remember the game King of Hearts from Lab02?
• It costs $1 to play this game each time. • If we win a jackpot, we gain$10.
## repeat Loops
• The repeat loops repeat a chunk of code until you tell them to stop (by hitting Escape) or until they encounter the command break, which will stop the loop.
• We can use a repeat loop to recreata a loop for the example above.