=begin
$i = 0
$num = 5
while $i < $num do
puts ("Inside the loop i = #$i")
$i += 1
end
=end
#while 修饰符
=begin
$i = 0
$num = 5
begin
puts("Inside the loop i = #$i")
$i += 1
end while $i<$num
=end
#until语句
=begin
$i = 0
$num = 5
until $i > $num do
puts ("Inside the loop i = #$i")
$i += 1
end
=end
=begin
$i = 0
$num = 5
begin
puts ("Inside the loop i = #$i")
$i += 1
end until $i > $num
=end
=begin
for i in 0..5
puts "Value of local variable is #{i}"
end
(0..5).each do |i|
if i>3 then
break
endå
puts "Value of local variable is #{i}"
end
=end
=begin next 语句 跳出本次循环 进行下一次迭代
for i in 0...5
if i < 2 then
next
end
puts "Value of local variable is #{i}"
end
=end
#redo语句 会重新启动这个最内部的循环迭代 而不检查循环条件 循环条件不会变化
=begin
for i in 0..5
if i<2 then
puts "Value of local variable is #{i}"
redo
end
end
=end
#retry语句 如果retry表达出现在rescue子句 则从开始重新开始 如果出现重试迭代 块,或体内的表达 重新启动迭代调用 迭代器的参数条件将重新计算 循环参数会变化 1.9版本以后不支持在循环中使用retry
for i in 1..5
retry if i > 2
puts "Value of local variable is #{i}"
end
来源:https://www.cnblogs.com/ToBeTheOne/p/5743639.html