FOR loops
For loops allow a certain operation to be repeated a fixed number of times and this is opposed to the While loop where the rep nr is not prefixed.
The syntax looks like this: for (name in vector) {commands}
variable name does not matter although you will see i quite often to skip certain elements in the loop: next
to break/stop the loop: break
Let us create the Fibonacci sequence, in this case the first 20 nunbers.
This is a famous sequence of numbers which is used e.g. in stock market predictions.
It starts with 2 x 1 and continues with the summation of the 2 preceding elements.
WHILE loop
if we want to repeat statements, but we do not yet know about the pattern of repetition.
as long as the condition holds the calculation is performed
x meets condition (command in curly brackets is performed)
syntax: while (condition) {commands}
# y does not meet condition
y = 6
while(y < 5){
y = y+10
print(y)
}
REPEAT loop
This loop is used infrequently - however sometimes it can be useful.the idea is to repeat a certain statement endlessly (difference to FOR) with the break statement we terminate the loop
syntax: repeat {statements} if (condition) break
note that the position of the print command determins if x-3 or x is printed
this can be seen in the last number output
For loops allow a certain operation to be repeated a fixed number of times and this is opposed to the While loop where the rep nr is not prefixed.
The syntax looks like this: for (name in vector) {commands}
for (i in 1:15) {print (i)}
for (z in 1:15) {print (z)}
variable name does not matter although you will see i quite often to skip certain elements in the loop: next
for(i in 1:12){
if(i==3)
next
print (i)}
to break/stop the loop: break
for(i in 1:12){
if(i==3)
break
print (i)}
Let us create the Fibonacci sequence, in this case the first 20 nunbers.
This is a famous sequence of numbers which is used e.g. in stock market predictions.
It starts with 2 x 1 and continues with the summation of the 2 preceding elements.
# with the first line we create an empty vector
Fibonacci = c()
# here we define the first two numbers
Fibonacci[1] = Fibonacci[2] = 1
# and here we create the loop to calculate positions 3 to 20
for (i in 3:20) {Fibonacci[i] = Fibonacci[i-2] + Fibonacci[i-1]}
Fibonacci
WHILE loop
if we want to repeat statements, but we do not yet know about the pattern of repetition.
as long as the condition holds the calculation is performed
x meets condition (command in curly brackets is performed)
syntax: while (condition) {commands}
x = 4
while(x < 5){
x = x+10
print(x)
}
# y does not meet condition
y = 6
while(y < 5){
y = y+10
print(y)
}
REPEAT loop
This loop is used infrequently - however sometimes it can be useful.the idea is to repeat a certain statement endlessly (difference to FOR) with the break statement we terminate the loop
syntax: repeat {statements} if (condition) break
x = 1
repeat {
x = x + 3
if (x>49)
break
print(x)}
note that the position of the print command determins if x-3 or x is printed
this can be seen in the last number output
x = 1
repeat {
x = x + 3
print(x)
if (x>49)
break}
R Programming – FOR loops, WHILE loop & REPEAT loop
Reviewed by Pubudu Dewagama
on
10:49:00 PM
Rating:
No comments: