Repeat loops

Intermediate R for Finance

Lore Dirick

Manager of Data Science Curriculum at Flatiron School

Loops

ch3_vid1_slides.003.png

1 tdfitness, My Daily Routine
Intermediate R for Finance

Loops

ch3_vid1_slides.004.png

1 tdfitness, My Daily Routine 2 John Rochelle, Water Cycle
Intermediate R for Finance

Repeat loop

i <- 0

repeat {
  i <- i + 1
  print(i)
}
1
2
3
4
.
.
.
# Infinite loop!
Intermediate R for Finance

Repeat loop

i <- 0

repeat {
  i <- i + 1
  print(i)

  if(i == 3) {
    break
  }
}
1
2
3
Intermediate R for Finance

Repeatedly check a stock price

stock_price <- 52.1
repeat {
  stock_price <- stock_price * runif(1, .99, 1.01)
  print(stock_price)
  if(stock_price > 52.5) {
    print("Stock price is above 52.5! Sell!")
    break
  }
}
52.0491
52.42412
51.92386
51.82161
52.19669
52.43577
52.71329
"Stock price is above 52.5! Sell!"
Intermediate R for Finance

Let's practice!

Intermediate R for Finance

Preparing Video For Download...