Loops are a basic element of all computer programming. In R, loops are somewhat less necessary than in many other languages because R can do vectorized calculations---calculations on all of the elements of a vector (or other data structure) simultaneously without a loop. Such calculations are often faster (potentially much faster) than those based on a loop, so loops should be avoided in R when possible. However, loops aren't always slower than vectorized calculations, and some calculations cannot be vectorized. Therefore loops still play an important role in R programming.
In this video we introduce the for loop, which is the most common form of loop among R programmers. We examine a couple of simple examples as well as one slightly more complex example that illustrates the Central Limit Theorem. You should follow along with Example 7 in Mod11Script.R when watching this video. The topic of vectorized computations will be covered in another video.
This video is essential viewing for the course.