A loop is a control structure that allows code to be executed repeatedly based on a given condition.
A while loop is a control structure that allows code to be executed repeatedly based on a given condition. The code within a while loop will continue executing while the condition evaluates truthy. While loops are often used when the number of iterations is unknown beforehand, they can be used in any situation where a loop is needed.
The condition is evaluated before the code within the loop is executed, which means that if the condition is false, the code within the loop will never be executed.
i = 0
while i < 3
puts i
i += 1
end
# Output:
# 0
# 1
# 2
When wanting to iterate over a string, you can loop over the length of the string.
str = "Hello"
i = 0
while i < str.size
puts str[i]
i += 1
end
# Output:
# H
# e
# l
# l
# o
Until loops work the same as while loops, except that the code within the loop will continue to execute until the condition evaluates to truthy.
It is the same as the difference between if
and unless
.
i = 0
until i == 3
puts i
i += 1
end
A common error with while loops is entering an "infinite loop," which never exits. An infinite loop can be caused by a condition never evaluates to truthy or falsey. This is often due to programmer error; for example, forgetting to increment the loop variable. But sometimes, an infinite loop is the clearest way to implement the logic of the program. Or it is vital to loop forever; for example, an HTTP server waits for an incoming connection, handles it, and then waits for the next connection.
The break
keyword can be used to exit a loop early.
This can be convenient when you want to exit a loop early under certain conditions.
i = 0
while i <= 3
puts i
i += 1
if i == 2
break
end
end
# Output:
# 0
# 1
The next
keyword can be used to skip to the next iteration of a loop.
This can be convenient when you want to skip specific iterations of a loop.
i = 0
while i <= 3
i += 1
if i == 2
next
end
puts i
end
# Output:
# 1
# 3
The type of the variable can be different under looping.
In the following example, the type of a
is Int32 | String
because it can be Int32
or String
under the first part of the loop.
The variable a
will only carry the type Int32 | String
in the first part of the loop since when the loop starts, it holds Int32
, and in the second iteration, it holds a String
.
This multiple-type system will be explained in detail later.
But since we are sure that the second part of the loop will only hold String
, the type of a' is
Stringunder the second part of the loop. At runtime, it will never simultaneously hold
Int32and
String`.
i = 0
a = 4
while i < 3
p typeof(a)
# => Int32 | String
a = "4"
p typeof(a)
# => String
i += 1
end
While can be used as an expression, which means it can be used as the value of a variable.
The value that is returned is the value given with the break
keyword.
It will not return the value of the last expression in the loop.
a = while true
break 1
end
p a
# => 1
i = 0
a = while i == 0
i += 1
1
end
p a
# => nil