r语言中的while循环_R编程中的While循环

r语言中的while循环

In addition to the for loop we discussed earlier, R also offers another kind of loop to perform iterative programming, namely the while loop.

除了前面讨论的for循环外 ,R还提供了另一种执行迭代编程的循环,即while循环。

In many scenarios, we don’t know exactly how many times we want our loop to run. In such cases, while is the most useful programming construct. A while loop runs as long as the condition we supply initially holds true.

在许多情况下,我们不知道到底希望循环运行多少次。 在这种情况下, while是最有用的编程结构。 只要我们最初提供的条件成立,就会运行while循环。

R中while循环的结构 (Structure of while loop in R)


while(anycondition){
#Do something
}

It is common for a while loop to keep making changes to the condition variable until a certain point. For example, we might want the loop to run while a variable value if TRUE.

通常,while循环会不断更改条件变量直到某个点。 例如,我们可能希望循环在变量值为TRUE

When a certain condition gets met inside the while loop, it can change the variable value to FALSE and terminate the loop. This terminating condition should be handled with care so as to not run into infinite loops. We will see some examples shortly.

在while循环内满足特定条件时,可以将变量值更改为FALSE并终止循环。 应谨慎处理此终止条件,以免陷入无限循环 。 我们将很快看到一些示例。

简单的while循环示例 (Simple while loop Example)


a=TRUE
while(a==TRUE){
  print("Entering loop")
  print("A is true")
  a=FALSE
}
  • This simple piece of code first sets the value of a Boolean variable a to TRUE.

    这个简单的代码首先将布尔变量a的值设置为TRUE
  • The while loop checks if the value of a is TRUE. Since it is true, it enters the loop and prints “A is true” first.

    while循环检查a的值是否为TRUE 。 由于它是正确的,因此进入循环并首先打印“ A is true ”。
  • Then a gets assigned to FALSE.

    然后将分配给FALSE
  • In the next loop run, the while loop checks the a value, which is not FALSE and therefore exits the loop right away.

    在下一个循环运行中,while循环检查a值,该值不是FALSE ,因此立即退出循环。
  • Let us look at the output.

    让我们看一下输出。

Output:

输出:


[1] "Entering Loop"
[1] "A is true"

The loop sentinel statement here gets printed only once, as the loop doesn’t get to run for the second time for a value FALSE.

这里的循环哨兵语句仅被打印一次,因为该循环不会第二次运行一个值FALSE。

在while循环中使用break语句 (Using break statement in the while loop)

Since while loops have a possibility of running into infinite loops and exhausting system memory, it is ideal to use break in several cases.

由于while循环有可能陷入无限循环并耗尽系统内存,因此在多种情况下使用break是理想的选择。

Let us look at an example.

让我们来看一个例子。

The most common computing application of loops is the generation of sequences. Suppose you wish to print the Fibonacci sequence until a particular limit, say till the number is less than or equal to 300. You surely don’t know how many iterations it will take to reach 300.

循环的最常见计算应用是序列的生成。 假设您希望打印斐波那契数列直到一个特定的限制,例如直到该数目小于或等于300。您肯定不知道达到300个数将需要进行多少次迭代。

The following is a naive way of displaying the Fibonacci sequence using a while loop.

以下是使用while循环显示斐波那契序列的幼稚方法。


f1=0
f2=1
sum=0
while(TRUE){
  sum <- f1+f2
  f1 <- f2
  f2 <- sum
  print(sum)
}

Chances are that you will only see exponential and Inf numbers on your console, which continue printing till you press the STOP button on the top left of the console.

很有可能您只会在控制台上看到指数和Inf编号,这些数字会继续打印,直到您按下控制台左上方的STOP按钮为止。

The problem here lies with the while(TRUE) part of the loop. We are specifying the compiler to run the loop forever and generate all the Fibonacci numbers. Therefore, we need to handle this loop by placing our condition inside the loop. Let us see a modified piece of code.

这里的问题在于循环的while(TRUE)部分。 我们指定编译器永久运行循环并生成所有斐波那契数。 因此,我们需要通过将条件放入循环中来处理此循环。 让我们来看一段经过修改的代码。


f1=0
f2=1
sum=0
while(TRUE){
  sum <- f1+f2
  f1 <- f2
  f2 <- sum
  if(sum>300){
    break
  }
  print(sum)
  
}

We have now handled the condition of the sum being greater than 300 using an if statement block above. Once the condition is met, we simply break the loop and come out of it.

现在,我们使用上面的if语句块处理了总和大于300的条件。 一旦满足条件,我们就简单地打破循环,摆脱循环。

Let us see the output now:

现在让我们看一下输出:


[1] 1
[1] 2
[1] 3
[1] 5
[1] 8
[1] 13
[1] 21
[1] 34
[1] 55
[1] 89
[1] 144
[1] 233

The next number in the sequence will be 377, which is greater than 300. But we have already checked for the sum being greater than 300 right before we print the sum and broke the loop because it is. Therefore we only get the sequence till 233.

序列中的下一个数字将是377,该数字大于300。但是在打印总和并因此中断循环之前,我们已经检查了总和是否大于300。 因此,我们只能得到序列直到233。

Now try displaying the sum value. Interestingly, it is 377 and not 233.

现在尝试显示总和值。 有趣的是,它是377,而不是233。


> sum
[1] 377

The sum value 377 is calculated and stored in the sum, but not printed since we broke out of the loop.

计算总和值377并将其存储在总和中,但由于我们退出循环,因此不进行打印。

翻译自: https://www.journaldev.com/35051/while-loop-in-r-programming

r语言中的while循环

  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值