There are many cases when one needs to do repetitive coding: carry out the same commands but on a different object/data. Writing loops is one of the best tools to carry out such repetition with only a few modifications to the codes. It also reduces the code duplication, which has three main benefits:
- It’s easier to see the intent of your code because your eyes are drawn to what’s different, not what stays the same.
- It’s easier to respond to changes in requirements. As your needs change, you only need to make changes in one place, rather than remembering to change every place that you copied and pasted the code.
- You’re likely to have fewer bugs because each line of code is used in more places.
(Hadley Wickham and Garrett Grolemund R for Data Science Ch. 21.1)
This lecture introduces students to imperative programming with for
and while
loops. Furthermore, it provides an exercise with sp500 dataset to calculate yearly and monthly returns.
Chapter 05, A: What likelihood of loss to expect on a stock portfolio? case study was a starting point to develop the exercise.
After successfully live-coding the material (see: loops.md
), students will know
- What is imperative programming and what is functional programming for iterations
- What is a for loop
- what are the possible inputs for an iteration vector
- how to measure CPU time
- what are the possible issues with the for-loop
- What is a while loop
- what are the possible drawbacks of a while loop
- how to use a for loop instead
break
command
- Calculate returns with different time periods.
Ideal overall time: 10-20 mins.
This is a relatively short lecture, and it can be even shorter if measuring CPU time and/or exercise is/are neglected.
Type: quick practice, approx 15 mins, together with lecture08-conditionals, lecture10-random-numbers, and lecture11-functions.
Check the common homework here.
- More materials on the case study can be found in Gabor's da_case_studies repository: ch05-stock-market-loss-generalize
- Hadley Wickham and Garrett Grolemund R for Data Science Chapter 21 provide further material on iterations, both imperative and functional programming.
- Jae Yeon Kim: R Fundamentals for Public Policy, Course material, Lecture 10 provides useful guidelines on iterations along with other programming skills.