September 2018
Beginner
206 pages
4h 27m
English
Versus the for loop, which walks through an iterator (usually, this is a sequence of numbers), a while loop will not iterate through a sequence of numbers for you. Instead, it requires you to add a line of code inside the body of the loop that increments or decrements your iterator, usually i. Generally, the syntax for a while loop is as follows:
while(test_expression){some_action}
Here, the action will only occur if the test_expression is TRUE. Otherwise, R will not enter the curly braces and run what's inside them. If a test expression is never TRUE, it's possible that a while loop may never run!
A classic example of a while loop is one that prints out numbers, such as the following:
i = 0while(i <= 5){ print(paste("loop", i)) ...Read now
Unlock full access