evoalgs-r-practise/loops.R

33 lines
569 B
R
Raw Normal View History

2024-04-30 08:03:37 +00:00
# For loop example
print("For Loop Output:")
for (i in 1:5) {
print(paste("Iteration", i))
}
# While loop example
print("While Loop Output:")
count <- 1
while (count <= 5) {
print(paste("Count is", count))
count <- count + 1
}
# Repeat loop example with break
print("Repeat Loop Output (with break):")
count <- 1
repeat {
if (count > 5) {
break
}
print(paste("Repeat count is", count))
count <- count + 1
}
# Using next to skip an iteration
print("For Loop with Next:")
for (i in 1:5) {
if (i == 3) {
next
}
print(paste("Iteration", i))
}