ruby循环

Ruby循环 (Ruby Loops)

Loops are comprised of sequentially group instructions which are executed repeatedly until a certain condition is met. Loops provide great help in making the programmer's task easier.

循环由顺序执行的组指令组成,这些指令重复执行直到满足特定条件为止。 循环为简化程序员的任务提供了很大的帮助。

Ruby中循环语句的类型 (Types of looping statements in Ruby)

Ruby supports the following types of loops:

Ruby支持以下类型的循环:

  1. The while loop

    while循环

  2. The for loop

    for循环

  3. The do...while loop

    do ... while循环

  4. The until loop

    直到循环

1)while循环 (1) The while loop)

In the while loop, the condition for which the loop will run is provided at the top with while keyword. It is one of the forms of the Entry control loop and the pointer gets out when the specified condition is met.

在while循环中,循环的运行条件在顶部提供了while关键字。 它是Entry控制循环的一种形式,当满足指定条件时指针会跳出。

When the number of repetitions is not fixed, it is recommended to use while loop.

当重复次数不固定时,建议使用while循环。

Syntax:

句法:

    while (condition )
# code to be executed
end

Example:

例:

num=5
while num!=0
puts "Number is greater than zero"
num-=1
end

Output

输出量

Number is greater than zero
Number is greater than zero
Number is greater than zero
Number is greater than zero
Number is greater than zero

In the above code, you can observe that the number of iterations was not fixed. It was dependent upon variable which is specified in the condition.

在上面的代码中,您可以观察到迭代次数不是固定的。 它取决于条件中指定的变量。

2)for循环 (2) The for loop)

for loop is different from while loop only in the context of syntax otherwise both of them are having the same functionality. It is one of the forms of the Entry control loop and the number of iterations must be specified before the execution of the loop. It repeats over a given range of numbers.

for循环与while循环仅在语法方面不同,否则它们都具有相同的功能。 它是Entry控制循环的一种形式,必须在执行循环之前指定迭代次数。 它在给定的数字范围内重复。

Syntax:

句法:

    for variable_name[, variable...] in expression [do]
# code to be executed
end

Example:

例:

i = "Includehelp"
for l in 1..7 do
puts i
end

Output

输出量

Includehelp
Includehelp
Includehelp
Includehelp
Includehelp
Includehelp
Includehelp

3)do ... while循环 (3) The do...while loop)

It is the kind of Exit control loop. It is very identical to while loop but with a difference that the condition is tested after the execution of specified statements. If you want that the expressions must execute at least for once, you should go for do...while loop.

这是一种退出控制循环。 它与while循环非常相同,但不同之处在于在执行指定语句之后测试条件。 如果希望表达式必须至少执行一次,则应执行do ... while循环。

Syntax:

句法:

    loop do
# code block
break if Condition
end

Example:

例:

num = 0
loop do
puts "Includehelp.com"
num+=1
if num==8
break
end
end

Output

输出量

Includehelp.com
Includehelp.com
Includehelp.com
Includehelp.com
Includehelp.com
Includehelp.com
Includehelp.com
Includehelp.com

4)直到循环 (4) The until loop)

until loop is the antonym of while loop as per the functionality. While loop is terminated when the condition becomes false but the until loop is terminated when the Boolean expression evaluates to be true. It is one of the examples of Entry control loop where the condition is specified before the execution of expressions.

根据功能,直到循环是while循环的反义词。 当条件变为假时,while循环终止,但是当布尔表达式的值为真时,直到循环终止。 它是Entry控制循环的示例之一,其中在执行表达式之前指定条件。

Syntax:

句法:

    until conditional [do]
# code to be executed
end

Example:

例:

num = 8
until num == 13 do
puts "Hi there! Welcome to the tutorial"
num+=1
end

Output

输出量

Hi there! Welcome to the tutorial
Hi there! Welcome to the tutorial
Hi there! Welcome to the tutorial
Hi there! Welcome to the tutorial
Hi there! Welcome to the tutorial

翻译自: https://www.includehelp.com/ruby/loops.aspx

ruby循环

ruby循环_Ruby循环相关推荐

  1. python无限循环条件循环_Python - 条件控制、循环语句 - 第十二天

    Python 条件控制.循环语句 end 关键字 关键字end可以用于将结果输出到同一行,或者在输出的末尾添加不同的字符,实例如下: Python 条件语句是通过一条或多条语句的执行结果(True 或 ...

  2. java byte 循环左移 循环右移 rotateLeft rotateRight

    java byte 循环左移 循环右移 rotateLeft rotateRight 1.概念. 循环左移: eg1:byte in = (byte) 0x01;[0000 0001]则循环左移2位后 ...

  3. 【MATLAB】流程控制 ( 循环结构 | for 循环 | while 循环 | 分支结构 | if end 分支结构 | if else end 分支结构 | switch case 分支结构 )

    文章目录 一.MATLAB 流程控制结构 二.for 循环 1.for 循环 2.嵌套 for 循环 三.while 循环 二.分支结构 1.if end 分支结构 2.if else end 分支结 ...

  4. shell条件测试操作 if分支 for循环 while 循环

    目录 条件测试操作 条件测试的基本用法: 字符串条件测试: 逻辑组合 数字的条件测试: 编写一个脚本: 对文件进行测试: if判断语句 单分支 双分支 多分支 编写一个脚本: 编写一个脚本: for循 ...

  5. python有限循环_Python循环

    # for循环 # ## 实例1: ## #基本语法 for i in range(100): print(i) #range(起始位,参数,步长) for j in range(1,100,2):# ...

  6. PHP如何用while实现循环,PHP 循环 -

    PHP 循环 - While 循环 循环执行代码块指定的次数,或者当指定的条件为真时循环执行代码块. PHP 循环 在您编写代码时,您经常需要让相同的代码块一次又一次地重复运行.我们可以在代码中使用循 ...

  7. python循环套循环_零基础学python 14 循环套循环:循环的嵌套

    大家可以回忆一下,之前我们学习过的循环结构.也就是for循环结构,今后我们还将学习另一种循环结构while结构,这里先不细讲.当初我们学习for循环结构的时候,有心的同学或许会发现,我们的for结构一 ...

  8. php函数内的循环,PHP 循环列出目录内容的函数代码

    PHP 循环列出目录内容的函数代码 复制代码 代码如下: function list_files($dir) { if(is_dir($dir)) { if($handle = opendir($di ...

  9. 程序语言python循环_Python 循环语句

    本章节学习Python的循环语句,程序在一般情况下是按顺序执行的. 编程语言提供了各种控制结构,允许更复杂的执行路径. 循环语句允许我们执行一个语句或语句组多次,下面是在大多数编程语言中的循环语句的一 ...

最新文章

  1. 【LeetCode每周算法】零钱兑换
  2. HFSS15.0安装步骤
  3. JS 常用函数二(改变HTML样式)
  4. WPF中查看PDF文件 - 基于开源的MoonPdfPanel (无需安装任何PDF阅读器)问题汇总
  5. ssm整合之配置applicationContext-service.xml
  6. 通过网页链接调用QQ临时会话实现
  7. 在Windows服务器上启用远程桌面连接
  8. Linux配置并编译内核
  9. 计算机开机切换用户界面,win7开机登录界面怎么设置?win7更换开机画面壁纸解决办法...
  10. MAT jvm 分析工具,mac,windows版本 下载地址
  11. 深入详细理解矩阵 (矩阵的加减乘、转置、共轭、共轭转置)
  12. python求奇偶数和_python奇偶数求和
  13. 手机拍照技巧(一:校园拍摄)
  14. 仿微博视频边下边播之封装播放器
  15. 小白组装电脑详细教程
  16. GitLab版本升级跨大版本
  17. Python3.8+OpenCV4 实现二维码扫码
  18. nodejs使用redis.js并进行封装,实现数据存取
  19. SEERC 2008 Problem A Stock Exchange
  20. 深度学习(Deep Ritz,Galerkin,PINN)求解偏微分方程(PDE)实现代码地址

热门文章

  1. .net 开发 html框架,Asp.net的开发框架
  2. 查找指定日期数据所在分区数据
  3. JxBrowser概述与简单应用
  4. Office快捷键大全之三(Access快捷键下篇)
  5. 设置了li(float:right),里面的li反过来显示 - 解决办法
  6. ffmpeg 纯静态编译,以及添加自定义库流程摘要
  7. Oracle 10g RAC 升级(CPU Update)之--升级CRS
  8. php读取checkbox数组值
  9. 昨天安装复习中遇到的问题小结
  10. 1030利用三层交换机实现VLAN间通信