博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ruby 嵌套函数_Ruby嵌套直到循环带有示例
阅读量:2529 次
发布时间:2019-05-11

本文共 2036 字,大约阅读时间需要 6 分钟。

ruby 嵌套函数

嵌套直到循环 (Nested until loop)

Alike for, , and , loop can also be nested for meeting the specific purpose. In this type of nesting, two until loops work in the combination such that at first, the outer loop is triggered which results in the execution of the inner loop. The outer Until loop is not invoked as long as the inner loop does not comes out to be true. It is a kind of Entry control loop which simply means that first the outer Boolean expression is processed; if it is false then the pointer will move to the inner loop to check inner condition. The whole execution will take place in the same manner.

与for, 和 , 循环也可以嵌套以满足特定目的。 在这种嵌套中,两个直到循环在组合中起作用,这样一来,首先触发外循环,从而执行内循环。 只要内部循环不成立,就不会调用外部的直到循环。 这是一种Entry控制循环,仅表示首先处理外部布尔表达式;然后进行处理。 如果为假,则指针将移至内部循环以检查内部条件。 整个执行将以相同的方式进行。

Syntax:

句法:

until conditional [do]        until conditional [do]            # code to be executed        end               # code to be executed    end

Example 1:

范例1:

=begin Ruby program to find the sum of numbers lying between two limits using nested until loop=endputs "Enter the Upper limit"ul=gets.chomp.to_iputs "Enter the Lower limit"ll=gets.chomp.to_iuntil ul==ll	num=ul	temp=ul	sum = 0	until num==0		#implementation of until loop		rem=num%10		num=num/10		sum=sum+rem	end	puts "The sum of #{temp} is #{
sum}" ul=ul-1end

Output

输出量

Enter the Upper limit1000Enter the Lower limit980The sum of 1000 is 1The sum of 999 is 27The sum of 998 is 26The sum of 997 is 25The sum of 996 is 24The sum of 995 is 23The sum of 994 is 22The sum of 993 is 21The sum of 992 is 20The sum of 991 is 19The sum of 990 is 18The sum of 989 is 26The sum of 988 is 25The sum of 987 is 24The sum of 986 is 23The sum of 985 is 22The sum of 984 is 21The sum of 983 is 20The sum of 982 is 19The sum of 981 is 18

Example 2:

范例2:

Pattern printing: Print the following pattern

图案打印:打印以下图案

0    01    012    0123    01234

Code:

码:

=begin Ruby program to print the given pattern.=endnum=0until (num==6)	j=0	until(j==num)		print j		j+=1	end	puts ""	num+=1end

翻译自:

ruby 嵌套函数

转载地址:http://mmxzd.baihongyu.com/

你可能感兴趣的文章
Java知多少(17)强调一下编程风格
查看>>
CSS3绘制的军曹giroro卡通图像
查看>>
【转】django 与 vue 的完美结合 实现前后端的分离开发之后在整合
查看>>
springboot集成activeMQ
查看>>
Flask项目之手机端租房网站的实战开发(十)
查看>>
adb常用命令
查看>>
[总结]编程中遇到的vc提示的一些警告
查看>>
Python学习笔记-EXCEL操作
查看>>
9.for循环
查看>>
百度PaddlePaddle再获新技能 智能推荐、对话系统、控制领域都能搞定!
查看>>
SELINUX setsebool
查看>>
C#的Socket程序(TCP/IP)总结
查看>>
java源码生成可运行jar
查看>>
用一个常见的生活小例子来解释和演示面向接口编程
查看>>
找出数组中两个只出现一次的数字
查看>>
【TYVJ水题三连发】1502 兴建高铁 1568 Rabbit Number 1673 位图
查看>>
centos 允许远程连接mysql
查看>>
最近几个月的感想
查看>>
CSS动画效果
查看>>
JS递归
查看>>