嘿,我想把数字增加1,直到某个结束点,然后再次从1开始。例如:
end_point = 3
1 2 3 1 2 3
seq(1,end_point)
…执行此操作至少一次,但我希望它在循环中重复,直到匹配某个条件。
下面是使用if语句的解决方案。
end_point = 3
seq(1,end_point)
check=1
some_conditions = length(c(1,2,3,1,2,3)) ## this is just to match your example, you can turn this into any condition
some_value = 0
while (check==1){
for (i in seq(1,end_point)){
print(i)
some_value = some_value+1
if (some_conditions == some_value){
check = 0
break}
}
}
本质上,只要'check'不变,while循环就会继续循环for循环,从而产生重复的1,2,3序列。可以设置特定的条件来改变'check'的值,从而打破while循环。break语句防止它完成重复序列。