R语言中while循环
R语言中while循环
1、测试1
a <- c(3,8,2,7,4,9)
i = 1
while (i <= length(a)) {
print(a[i])
i = i + 1
}
2、测试2
f <- NA
f[1] <- f[2] <- 1
i <- 3
while (i <= 10) {
f[i] = f[i - 1] + f[i - 2]
i = i + 1
}
f
3、测试3
f1 <- 0
f2 <- 1
sum = 0
while (TRUE) {
sum <- f1 + f2
f1 <- f2
f2 <- sum
if(sum > 300){
break
}
print(sum)
}