Ruby while 循环
while
循环只要指定的条件为 true
,就会重复执行一段代码。
while
循环的语法是
while condition
# Body of loop
end
这里,
while
循环首先会评估condition
。- 如果
condition
被评估为true
,则执行循环体。 - 然后,再次评估
condition
。 - 只要
condition
被评估为true
,这个过程就会一直持续。 - 如果
condition
被评估为false
,循环就会停止。
while 循环流程图

示例 1:显示从 1 到 3 的数字
# Initialize variable i
i = 1
# Loop runs until i is less than 4
while i < 4
puts i
# Increase value of i by 1
i += 1
end
输出
1 2 3
以下是循环每次迭代的程序工作方式
变量 | 条件:i | 操作 |
---|---|---|
i = 1 |
true |
打印 1。i 增加到 2。 |
i = 2 |
true |
打印 2。i 增加到 3。 |
i = 3 |
true |
打印 3。i 增加到 4。 |
i = 4 |
false |
循环终止。 |
要了解更多关于循环条件的信息,请访问 Ruby 比较运算符和逻辑运算符。
示例 2:仅正数的总和
sum = 0
# Loop as long as input number is positive
while true
print "Enter a number: "
user_input = gets.chomp
# Convert string to integer
input_number = user_input.to_i
# Exit loop if negative
break if input_number < 0
# Add positive number
sum += input_number
end
puts "The sum is #{sum}"
输出
Enter a number: 2 Enter a number: 4 Enter a number: -3 The sum is 6
在上面的程序中,
- 我们使用
gets
从用户那里获取输入。 - 我们使用
.to_i
将输入字符串转换为整数。 - 如果数字为负数,则使用
break
停止循环。 - 否则,将数字添加到
sum
中。
注意:在 Ruby 中,输入始终作为字符串读取。因此,您必须使用 .to_i
将它们转换为数字才能进行数学运算。
Ruby until 循环
until
循环会重复执行一段代码,直到指定的条件变为 true
。
它基本上与 while
循环是相反的。
until
循环的语法是
until condition
# Body of loop
end
这里,
until
循环会评估condition
。- 如果
condition
被评估为false
,则执行循环内的代码。 - 每次迭代后,会再次检查
condition
。 - 只要
condition
被评估为true
,这个过程就会一直持续。 - 如果
condition
被评估为true
,则循环终止。
until 循环的流程图

示例 3:显示从 3 到 1 的数字
i = 3
# Loop runs until i becomes 0
until i == 0
puts i
# Decrease value of i by 1
i -= 1
end
输出
3 2 1
这里,i 的初始值为 3。然后,我们使用 until
循环迭代 i 的值。以下是循环每次迭代的工作方式
操作 | 变量 | 条件:i == 0 |
---|---|---|
打印 3。i 减为 2。 | i = 2 |
false |
打印 2。i 减为 1。 | i = 1 |
false |
打印 1。i 减为 0。 | i = 0 |
true |
循环终止。 | - | - |
while 和 until 循环之间的区别
while
和 until
之间的区别在于它们在相反的条件下运行。
Ruby while 循环
while
循环只要 condition
为 true
就会运行。例如,
i = 5
# False condition
# Body not executed
while i > 10
print "#{i}, "
i += 1
end
# No output
此程序将不会产生任何输出,因为循环只要 i
大于 10 就会运行。但在这里,i
是 5,因此小于 10。
因此,循环条件为 false
,循环会立即终止,不执行任何操作。
Ruby until 循环
另一方面,until
循环只要条件为 false
就会执行其主体。例如,
i = 5
# False condition
# Body is executed
until i > 10
print "#{i}, "
i += 1
end
输出
5, 6, 7, 8, 9, 10,
如您所见,条件 i > 10
为 false
,因为 i
是 5,小于 10。因此,循环会打印从 5 到 10 的数字。
当 i
变为 11 时,条件 i > 10
变为 true
,循环终止。
示例 4:正数的总和
sum = 0
input_number = 0
# Run the loop at least once
# Keep adding numbers until the user enters a negative value
until input_number < 0
# Add the current number to the sum
sum += input_number
# Ask the user to enter a number
print "Enter a number: "
user_input = gets.chomp
# Convert the input string to an integer
input_number = user_input.to_i
end
# Finally, display the sum
puts "The sum is #{sum}"
输出
Enter a number: 2 Enter a number: 4 Enter a number: -3 The sum is 6
在上面的程序中,until
循环提示用户输入一个数字。
只要我们输入正数,循环就会将它们相加并要求更多输入。
如果我们输入一个负数,循环将终止,而不会将负数相加。
更多关于 Ruby while 循环
无限 while
循环是指循环无限运行的条件,因为其条件始终为 true
。例如,
i = 1
# Condition is always true
while i < 5
puts i
end
此外,这里有一个无限 until
循环的示例
i = 1
# Condition never becomes true
until i > 5
puts i
end
在这两种情况下,条件都不会停止循环,因此代码会无限运行。
注意:无限循环可能导致程序崩溃。因此,请避免无意中创建它们。
1. for 循环
当我们知道所需的迭代次数时,我们使用 for
循环。例如,
# Display "hi" 3 times
for i in 1..3
puts "hi"
end
puts "bye"
输出
hi hi hi bye
在这里,我们知道需要打印 "hi"
三次。因此,我们使用了 for
循环而不是 while
循环。
2. while 循环
与此同时,当我们需要的终止条件可能会发生变化时,我们使用 while
循环。例如,
# Boolean variable to use as loop condition
is_display = true
# Variable to store user input
user_choice = nil
# Display "hi" as long as user wants
# Basically, run the loop as long as is_display is true
while is_display
puts "hi"
# Get user input
print "Print hi again? y for yes: "
user_choice = gets.chomp
# Set is_display to false if user_choice is not "y"
is_display = false if user_choice != "y"
end
puts "bye"
输出
hi print hi again? y for yes: y hi print hi again? y for yes: y hi print hi again? y for yes: n bye
在上面的程序中,我们让用户随意打印 "hi"
的次数。
由于我们不知道用户的决定,所以我们使用 while
循环而不是 for
循环。