R while loop

While loop runs until condition becomes False. . To create a while loop, follow while by a condition and a block of code, like this:

while (condition) {
code
}

while will rerun condition, which should be a logical test, at the start of each loop. If condition evaluates to TRUE, while will run the code between its braces. If condition evaluates to FALSE, while will finish the loop.

Examples:

i<-0 # Define variable i with 0.

while(i<10) { print(i) i=i+1 }

Output:

[1] 0
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9

Powered by WordPress