ruby循环_Ruby循环

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循环

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543708.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

后缀数组 TYVJ P1860 后缀数组

/*P1860 后缀数组时间: 1000ms / 空间: 131072KiB / Java类名: Main描述 我们定义一个字符串的后缀suffix(i)表示从s[i]到s[length(s)]这段子串。后缀数组&#xff08;Suffix array&#xff09;SA[i]中存放着一个排列&#xff0c;满足suffix(sa[i])<suffix(sa[i1]) 按照字典…

Binary XML file line #2: You must supply a layout_height attribute inflate

Android开发中遇到的奇葩问题之一&#xff1a;java.lang.NullPointerException&#xff0c;java.lang.RuntimeException:Binary XML file line #2: You must supply a layout_height attribute inflate&#xff0c;遇到这个问题说明你在非主流上测试&#xff0c;或者说是在部分…

量词逻辑量词里面的v表示?_知识表示能力问答中的人工智能量词(MCQ)

量词逻辑量词里面的v表示&#xff1f;1) How many types of quantifiers are there that are used to represent knowledge? 3 types2 typesUser can define as many quantifiers he wantsNone of the above Answer & Explanation Correct answer: 22 types There are two…

给定数组A []和数字X,请检查A []中是否有对X | 使用两个指针算法,O(1)空间复杂度| 套装2...

Prerequisite: 先决条件&#xff1a; Hashing data structure 散列数据结构 Given an array A[] and number X, check for pair in A[] with sum X | using hashing O(n) time complexity | Set 1 给定数组A []和数字X&#xff0c;请检查A []中是否有对X | 使用哈希O(n)时间复…

集合操作(三)Set

2019独角兽企业重金招聘Python工程师标准>>> Set集合 HashSet 哈希表保证元素的唯一性依赖于两个方法一个是hashCode方法一个是equals方法 如果两个对象的hashCode值相同,并且调用该对象的equals方法返回的是true的时候,那么就说明两个对象是相同的 结论&#xff1a…

mcq 队列_MCQ | 软件程序分析工具和组件分类| 免费和开源软件

mcq 队列Q1. Which of the following analysis methods come under Static Analysis Tools? Q1。 静态分析工具包含以下哪些分析方法&#xff1f; Code Walkthrough 代码演练 Code Inspection 代码检查 None of the Above 以上都不是 Both a. & b. 两者都 &#xff06;b。…

samba部署小结

[rootOracle ~]# yum install samba-swat -y[rootOracle ~]# yum install samba-client 客户端工具主配置文件&#xff1a;[rootOracle ~]# cat /etc/samba/smb.conf |grep -v "#"|grep -v "^$"|grep -v ";"[global]workgroup …

JAVA调用动态链接库

上一篇《JAVA本地接口&#xff08;JNI&#xff09;》中介绍了JAVA的JNI技术&#xff0c;通过JAVA自有的方式调用动态链接库&#xff0c;这一篇将继续为大家介绍使用其他方式调用动态链接库。 首先&#xff0c;我们编写一个用于测试的链接库 头文件 print.h #ifdef DLL_IMPLEME…

数组重复次数最多的元素递归_在不使用递归的情况下计算链接列表中元素的出现次数...

数组重复次数最多的元素递归Solution: 解&#xff1a; Input: 输入&#xff1a; A singly linked list whose address of the first node is stored in a pointer, say head and key is the data of which we have to count the number of occurrences. 一个单链表 &#xff…

DshanMCU-R128s2芯片外设支持列表

LCD 显示屏 厂商分辨率型号接口FPS100ask480 x 320Dshan_Display ModuleSPI60 摄像头 Sensor 厂商分辨率型号Size接口FPSGalaxyCoreVGA, 640 x 480GC03081/6.5DVP30GalaxyCoreUXGA, 1616 x 1232GC21451/5DVP13

第6周 搜索与排序

1 查找里程 给你这样一张里程表&#xff0c;如何写一个程序&#xff0c;输入两地的地名&#xff0c;能输出期间的里程&#xff1f; #include <stdio.h> #include <string.h> #define C_LEN 30typedef struct city {char name1[C_LEN];char name2[C_LEN];int distan…

(转) Twisted :第十九部分 改变之前的想法

2019独角兽企业重金招聘Python工程师标准>>> 简介 Twisted是一个正在进展的项目,它的开发者会定期添加新的特性并且扩展旧的特性. 随着Twisted 10.1.0发布,开发者向 Deferred 类添加了一个新的特性—— cancellation ——这正是我们今天要研究的. 异步编程将请求和响…

stl list 删除元素_删除所有出现的元素,并从列表中删除一些特定的元素。 C ++ STL...

stl list 删除元素list.remove()和list.remove_if()函数 (list.remove() and list.remove_if() functions) remove() function is used to remove all occurrences of a given element from the list and function remove_if() is used to remove set of some specific element…

Mac 获取 Brew

2019独角兽企业重金招聘Python工程师标准>>> 终端输入 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 转载于:https://my.oschina.net/fdstudio/blog/610680

express 项目生成器_用于项目的Express模板生成器(2)| 应用程序结构研究

express 项目生成器Hello! In express template generator for your projects (1), we looked at express generator and how we can start an express application with stressing to build a brand new structure of all required files. 你好&#xff01; 在针对您的项目的E…

简单的block

int multi 7; int (^myBlock)(int) ^(int num){ return num * multi; }; int result myBlock(5); NSLog("结果是&#xff1a;%d",result);//输出结果是&#xff1a; 结果是&#xff1a;35 void (^printBlock)(NSS…

c# 浮点数十六进制字符串_从C#中包含十六进制值的字符串数组中打印整数值...

c# 浮点数十六进制字符串将十六进制字符串数组转换为整数 (Converting array of hexadecimal strings to integers) Let suppose you have some of the strings (i.e. array of strings) containing hexadecimal values like "AA", "ABCD", "ff21&quo…

Linux 服务器中文乱码编码解决

Linux环境的ECS中&#xff0c;若出现如下中文显示为乱码的情况。 一般原因如下: 1. 未安装中文语言包 2. 未设置正确的默认语言 3. SSH 终端未正确配置 本文以Centos 6.5为例&#xff0c;演示如何解决中文乱码问题。 1. 使用 locale -a |grep zh_CN查看系统是否已经安装…

Python | 如何强制除法运算为浮点数? 除数一直舍入为0?

Until the python version 2, the division of two integers was always being rounded down to 0. 在python版本2之前&#xff0c; 两个整数的除法总是四舍五入为0 。 Consider the below example, being executed in python version 2.7, 考虑下面的示例&#xff0c;该示例在…

Python程序输入一个字符串并查找总数的大写和小写字母

Given a string str1 and we have to count the total numbers of uppercase and lowercase letters. 给定字符串str1 &#xff0c;我们必须计算大写和小写字母的总数。 Example: 例&#xff1a; Input: "Hello World!"Output:Uppercase letters: 2Lowercase lette…