1. While loop
In this video I'll be talking about while loops. The while loop is somewhat similar to the if statement because it executes the code inside if the condition is true. However, as opposed to the if statement, the while loop will continue to execute this code over and over again as long as the condition is true.
2. while loop
The syntax of a while loop is pretty similar to the if statement, as you can see here.
Let's have a look at a very simple example: we'll simply make R increment a counter until it reaches the value 7, my lucky number. We start by defining the variable ctr, short for counter, and setting it to 1.
Let's first set the 'condition' of the while loop, without worrying about the expressions inside it.
We want the while loop to execute as long as the ctr variable is less than or equal to 7. For the initial value of ctr equal to 1, the condition will evaluate to TRUE, but also for other values, such as 3, -5 and 7, this condition will be TRUE.
3. while loop
Next up is the expression. What do we want to while loop to do on every run? We want some information on how the while loop is progressing, so we'll throw in a print statement, together with the paste function.
If ctr equals 2, for example, this expression will print out "ctr is set to 2". We're not done yet! We still have to add another line of code to inform R that we want to increment the ctr variable on every run
4. while loop
We add ctr assign operator ctr + 1 to the loop code. Let's first try to guess how R will handle this while loop. Before R arrives at the while loop, ctr will be 1. The condition evaluates to TRUE, so the code inside the while loop gets executed.
5. while loop
R will print "ctr is set to 1", and then set ctr to ctr + 1; so ctr now equals 2.
6. while loop
Now, as opposed to the if statement, R takes another look at the condition ctr less than or equal to 7. The current value of ctr is 2 so the condition is TRUE. R executes the code inside again, prints out "ctr is set to 2" and increments the ctr variable. This will go on for ctr equal to 3, 4, 5 and 6. But what happens after ctr is set to 7 on the 6th run?
7. while loop
R checks the while loop's condition: it's still TRUE, because 7 is less than or equal to 7. R prints out ctr is set to 7 and then increments the ctr.
8. while loop
Now, the condition will be checked once more. But this time ctr will be equal to 8, which is greater than 7, so the condition evaluates to FALSE, forcing R to abandon the while loop.
9. while loop
Curious if our abstract thinking was correct? We'll simply execute the R code and can find out.
Indeed, the "ctr is set to" sentences are printed out for numbers 1 to 7. If we now check the value of ctr we see that indeed, ctr is equal to 8. 8 is the first value for ctr for which the condition fails, so R does not increment ctr further.
10. while loop
The line of code to increment ctr is crucial. Suppose we remove this line.
11. Infinite while loop
If we now run this code in R, the line "ctr is set to 1" would be printed indefinitely, until we stop the session manually with Control C. Why? Because ctr does not get updated; this would mean that the condition is always true, and R keeps on re-executing the code in the while loop. You'll have to hit the stop sign in your R console to stop this. What I truly want to say here: always make sure your while loop will end at some point!
12. break statement
There's one more thing I want to discuss before you get started with the exercises. The break statement.
The break statement simply breaks out of the while loop: when R finds it, it abandons the currently active while loop.
Suppose we want R to stop our while loop from before as soon as the value of ctr is divisible by 5. We can do this with a break statement.
If we now run this piece of code, the sentence is only printed out 4 times, for the ctr values 1 to 4. If we check out the ctr variable, it is equal to 5, because for ctr equal to 5, the condition that checked if ctr was divisible by 5 became TRUE, and the while loop was abandoned.
13. Let's practice!