ruby 嵌套函数

嵌套循环 (Nested for loop)

Nesting of for loop means one for loop is available inside another for loop. It means that there are two loops, then the first one is an outer loop and the second one is the inner loop. Execution will take place in the manner that first the outer for loop is triggered, then if the condition is matched the pointer will be passed to the inner loop. Here also if the condition is satisfied, the inner for loop body will be executed until specified condition does not comes out to be false. Once the inner loop completes its execution, the pointer will be passed back to the outer for loop for its successful execution.

嵌套for循环意味着一个for循环可在另一个for循环内使用 。 这意味着有两个循环,然后第一个是外部循环,第二个是内部循环。 执行将以首先触发外部for循环的方式进行,然后如果条件匹配,则指针将被传递到内部循环。 同样,如果满足条件,则将执行内部for循环主体,直到指定条件不为假为止。 内部循环完成执行后,指针将被传递回外部for循环以成功执行。

In Ruby, Nesting of for loop can be done with the help of the following syntax:

在Ruby中,可以使用以下语法来完成for循环的嵌套

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

Example 1:

范例1:

=begin
Ruby program to demonstrate nested for loop
=end
puts "Enter upper limit"
ul=gets.chomp.to_i
puts "Enter Lower limit"
ll=gets.chomp.to_i
for i in ll..ul do
for i in 0..3 do
puts "Inner loop triggered"
end
puts "Outer loop triggered"
end

Output

输出量

Enter upper limit
90
Enter Lower limit
85
Inner loop triggered
Inner loop triggered
Inner loop triggered
Inner loop triggered
Outer loop triggered
Inner loop triggered
Inner loop triggered
Inner loop triggered
Inner loop triggered
Outer loop triggered
Inner loop triggered
Inner loop triggered
Inner loop triggered
Inner loop triggered
Outer loop triggered
Inner loop triggered
Inner loop triggered
Inner loop triggered
Inner loop triggered
Outer loop triggered
Inner loop triggered
Inner loop triggered
Inner loop triggered
Inner loop triggered
Outer loop triggered
Inner loop triggered
Inner loop triggered
Inner loop triggered
Inner loop triggered
Outer loop triggered

You can observe in the above example that first the outer for loop is invoked then the pointer has moved to inner loop and it is printing the message for times.

您可以在上面的示例中观察到,首先调用了外部for循环,然后指针移至了内部循环,并且多次打印该消息。

Once the inner for loop completes its task, the pointer goes back to the outer loop.

内部for循环完成其任务后,指针将返回到外部循环。

Example 2:

范例2:

Patterns can also be printed using nested for loop. Now let us have a quick view of how the following pattern can be printed.

也可以使用嵌套的for循环来打印图案。 现在让我们快速了解如何打印以下图案。

*
**
***
****
*****

Code:

码:

=begin
Ruby program to print a pattern using nested for loop
=end
for i in 1..5 do
for j in 1..i do
print "*"
end
puts ""
end

翻译自: https://www.includehelp.com/ruby/nested-for-loop-with-examples.aspx

ruby 嵌套函数

ruby 嵌套函数_Ruby嵌套有示例的循环相关推荐

  1. ruby 嵌套函数_Ruby嵌套直到循环带有示例

    ruby 嵌套函数 嵌套直到循环 (Nested until loop) Alike for, while, and do...while, until loop can also be nested ...

  2. ruby 执行函数_Ruby at()函数

    ruby 执行函数 Ruby中的at()函数 (at() function in Ruby) If you are working with arrays in Ruby, sometimes you ...

  3. matlab if嵌套函数,MATLAB嵌套函数的应用

    嵌套函数在求解积分上限中的应用 例1如下述积分表达式,已知a.e和l,如何求得β0? 本例关于β的积分结果不能解析表达,需要数值积分来做,同时还要求一个非线性方程.代码如下: function sol ...

  4. ruby hash方法_Ruby中带有示例的Hash.rehash方法

    ruby hash方法 Hash.rehash方法 (Hash.rehash Method) In this article, we will study about Hash.rehash Meth ...

  5. ruby hash方法_Ruby中带有示例的Hash.invert方法

    ruby hash方法 Hash.invert方法 (Hash.invert Method) In this article, we will study about Hash.invert Meth ...

  6. ruby hash方法_Ruby中带有示例的Hash.select方法

    ruby hash方法 哈希选择方法 (Hash.select Method) In this article, we will study about Hash.select Method. The ...

  7. ruby hash方法_Ruby中带有示例的Hash.flatten方法

    ruby hash方法 哈希平化方法 (Hash.flatten Method) In this article, we will study about Hash.flatten Method. T ...

  8. ruby hash方法_Ruby中带有示例的Hash.default(key = nil)方法

    ruby hash方法 Hash.default(key = nil)方法 (Hash.default(key=nil) Method) In this article, we will study ...

  9. ruby hash方法_Ruby中带有示例的Hash.length方法

    ruby hash方法 哈希长度方法 (Hash.length Method) In this article, we will study about Hash.length Method. The ...

最新文章

  1. android dumpsys 分析,Android开发调试性能分析工具:dumpsys
  2. php获取当前系统配置文件,thinkphp5.1+配置文件结构及获取
  3. redirect路由配置 vue_Web前端:Vue路由进阶配置
  4. cctype 头文件定义 函数列表
  5. oracle返回当前日期函数,oracle 日期时间函数使用总结
  6. 笔记本一接上HDMI转VGA转换器就黑屏,无法操作连接显示器
  7. sql server 2000(迷你sql2000) jdbc驱动
  8. 【论文解读】深度残差网络去雨模型cvpr_Removing rain from single images via a deep detail network
  9. cad画直线长度与实际不符_CAD问题,画线长度不对?
  10. 利用python做一个超简单的抽签器
  11. 用Python实现温度转换程序
  12. C语言编程>第十七周 ⑤ 请补充fun函数,该函数的功能是:用来求出数组的最小元素在数组中的下标并存放在k所指的存储单元。
  13. python立方根求解_python – 如何获得立方根的整数?
  14. 绘制 polygons and polylines:OpenCV版本
  15. 最新版本供需指标介绍,自动识别有价值的K线,超级好用
  16. Edge浏览器中不输oneTab的标签整理插件
  17. 清北学堂day1考试
  18. Maple学习(一)Maple的安装
  19. Cover Letter 写作技巧
  20. shell批量修改文件名字 重命名 MD5+文件后缀

热门文章

  1. lintcode Permutation Index
  2. js连续指定两次或者多次的click事件(解决办法)
  3. (二)双线性插值python实现
  4. oracle进程瞬间暴增,oracle goldengate ogg 源段传输进程lag延迟不断增加的原因?
  5. python编写递归函数和非递归函数、输出斐波那契数列_分别用非递归和递归的方法编写函数求斐波那契数列第n项。斐波那契数列1,1,2,3,5,8,13,…...
  6. golang 安全的tcp server_Go 语言使用 TCP_NODELAY 控制发包流量
  7. Pytorch离线安装 matlibplot
  8. Redis(六):Set集合数据类型详解
  9. sql语言特殊字符处理
  10. 路由器DHCP和DHCP中继的配置