From the course: R for Data Science: Lunch Break Lessons

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

while loop

while loop

- [Instructor] With R, you're commonly going to want to use vector math but there are times when you need a while loop and R provides a while loop structure, so let's take a look at how that works. With any while loop, you want to initialize your vector, so in this case, I've created bobsYourUncle, a vector, and assigned the value of one. So you can see in the upper right-hand corner, bobsYourUncle equals one. In line three through six, I've created a while loop. What it says is while bobsYourUncle is less than five, I want you to print the value of bobsYourUncle and then I want you to add one to bobsYourUncle. So let's go ahead and click on line three and select run. And you can see that I get the values one, two, three and four. So what happened was it tested it, is bobsYourUncle less than five? And in the first case, bobsYourUncle is equal to one, so it prints it. It goes all the way up until bobsYourUncle is…

Contents