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+f2f1 <- f2f2 <- sumprint(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+f2f1 <- f2f2 <- sumif(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循环

r语言中的while循环_R编程中的While循环相关推荐

  1. 编程c语言中,向上取整函数_C编程中的函数

    编程c语言中,向上取整函数 什么是功能? (What is a Function?) A Function is a block of statements that performs a speci ...

  2. c语言中值程序,编程c语言中,向上取整函数_C编程中的函数

    编程c语言中,向上取整函数 什么是功能? (What is a Function?) A Function is a block of statements that performs a speci ...

  3. 编程c语言中,向上取整函数_C编程中的函数–第3部分

    编程c语言中,向上取整函数 Read: Functions in C Programming – Part 2 So far we have learnt about the simplest use ...

  4. R语言中的esttab命令_R语言︱基本函数、统计量、常用操作函数

    先言:R语言常用界面操作 帮助:help(nnet) = ?nnet =??nnet 清除命令框中所有显示内容:Ctrl+L 清除R空间中内存变量:rm(list=ls()).gc() 获取或者设置当 ...

  5. r语言中which的使用_R语言绘图 | 使用pheatmap快速绘制热图

    欢迎关注微信公众号"生信小王子"! 热图是我们展示数据时常用的图形,今天小编教大家使用"pheatmap" 快速绘制热图. 首先,我们需要准备输入文件.比如,我 ...

  6. c语言中sqrt和disc,C ++编程中的Sqrt,sqrtl和sqrtf

    数学课 本文演示了数学类基本函数sqrt(),sqrtl()和sqrtf()的用法,以分别精确地计算double,long和float类型变量的平方根.C ++的Math类提供了广泛的函数来计算数学计 ...

  7. r语言for循环的c(),R语言中for循环的并行处理方式

    前言 本文用于记录笔者在将R语言中的for语句并行化处理中的一些问题. 实验 这里使用foreach和doParallel包提供的函数实现for语句的并行处理. for语句脚本 func return ...

  8. c语言中什么语句的作用是结束本次循环,c语言中结束本次循环的语句是什么

    c语言中结束本次循环的语句是"continue".控制语句continue只能作用于循环体中,其作用是结束本次循环,跳到判断循环的位置,即重新开始下一次循环. 本教程操作环境:wi ...

  9. r语言中paste函数_R中的paste()函数-简要指南

    r语言中paste函数 Using the paste() function in R will be straight and simple. In this tutorial let's see ...

最新文章

  1. pandas为dataframe所有的列名称名添加前缀(add_prefix)
  2. 公众号接口,memcached缓存
  3. Git版本控制管理系统_企业实战上篇
  4. linux卸载tar安装的erlang包,linux - 从tar安装erlang导致错误,想知道如何指定文件夹 - 堆栈内存溢出...
  5. 使用SecretScanner发现容器镜像和文件系统中的敏感数据
  6. VS2017 社区版 许可证过期
  7. linux 定时器 库,linux 定时器
  8. 学校管理系统服务器,校园信息管理系统(MIS)
  9. 多线程同步有几种实现方法C语言,线程同步与并发
  10. html图片滤色,CSS3图片混合(Blend)效果详解
  11. luoguP4188 [USACO18JAN]Lifeguards S
  12. luckysheet 只读模式和编辑模式
  13. python画地图经纬度_如何用python画地图上的标注线?
  14. WebRTC中的信令和内网穿透技术 STUN / TURN
  15. msvcr120.dll丢失怎样修复?msvcr120.dll文件修复办法
  16. 黑马JAVA P104 继承后构造器的特点、this、super使用总结
  17. arp嗅探(windows)
  18. go的beego搭建
  19. acc 蓝牙_蓝牙耳机=音质渣?四大音质超赞的蓝牙耳机推荐
  20. 单相半波可控整流电路实验报告matlab,单相桥式全控整流电路MATLAB仿真 实验报告(上).doc...

热门文章

  1. Mysql 基于 Amoeba 的 读写分离
  2. dspic flash不够后,选择优化等级
  3. 动态SQL实现批量删除指定数据库的全部进程
  4. Linux下测试PHP和MySQL是否正确安装
  5. Sql 语句:显示 Sql Server 中所有表中的信息
  6. 数据结构与算法(Python)第四天
  7. ROS工作空间和程序包创建
  8. 一个优质的Vue组件库应该遵循什么样的设计原则
  9. 【BZOJ-1097】旅游景点atr SPFA + 状压DP
  10. HDU 5281 Senior's Gun 贪心