In a Ruby program, each statement is sequentially executed. One comes after another. But a looping construct modifies the flow of control—it makes some code repeat.
In Ruby the while
-loop is one construct. Until is the while
-loop's negative friend. While continues when the condition is true, "until" when false.
Here we use a simple while
-loop. We introduce an iteration variable—this has the identifier "I." We start this variable at 10.
while
-loop, we must be careful to avoid an infinite loop. The iteration variable must cause the loop to terminate.# The iteration variable. i = 10 # A while-loop. while i < 50 print "i: ", i, "\n" # Increment. i += 10 endi: 10 i: 20 i: 30 i: 40
An until-loop keeps iterating until the condition evaluates to true. The while
-loop continues until the condition is false. This is a syntax change.
while
-loop. With logic, we must ensure the loop is not infinite.# Start at five. index = 5 # Continued own until zero reached. until index == 0 print "Index: ", index, "\n" index -= 1 endIndex: 5 Index: 4 Index: 3 Index: 2 Index: 1
For
-loopWe use this loop to iterate over a range of numbers. We specify the iteration variable first. And then we indicate a range of numbers. This does not need to be constant.
# Loop over values 0 through 5. for i in 0..5 puts i end0 1 2 3 4 5
When this statement is encountered, the loop terminates. Usually loops are clearest when the endpoint is clearly specified in the loop condition. But sometimes this is not possible.
break
statement is often helpful in a while-true
loop, which would continue infinitely unless manually stopped.while
-loop continues printing the value until the if
-statement evaluates to true. Then the break
statement stops the loop.i = 0 while true # Break loop if variable is 5 or greater. if i >= 5 break end # Display variable and increment. puts i i += 1 end0 1 2 3 4
With this keyword, we can skip to the loop's next iteration. No further statements are executed for the current one. Unlike break
, next does not terminate the loop.
string
in an array. We skip to the next iteration (with next) if a length exceeds 3.array = ["cat", "dog", "plant", "horse"] # Loop over values 0 through 3. for element in array # Next iteration if length is too big. if element.length > 3 next end # Display element. puts element endcat dog
Loops support the redo statement. Redo terminates the current iteration, but it does not move to the next one. It restarts from the beginning of the loop, at the same iteration.
for
-loop.# Loop over values 0 through 3. for a in 0..3 # Get random number. i = rand 0..4 # Display iteration variable. puts a # Redo loop if not 2. redo unless i == 2 # Display done after each iteration. puts "DONE" end0 DONE 1 1 1 1 DONE 2 2 2 2 2 2 2 2 DONE 3 3 3 DONE
In looping constructs, the do
-keyword is optional. It may also have more symmetry with iterator blocks, which is an advantage. Do is used in other contexts, like iterators.
count = 3 # Use while with optional do-keyword. while count >= 0 do puts(count) # Decrement count. count -= 1 end3 2 1 0
A loop statement must have an end to it—the end statement is required. This is also true for methods and other kinds of blocks.
keyword_end
" when end is omitted.i = 0 # This loop is correct except it has no end-statement. while i < 10 puts(i) i += 2/test.rb:9: syntax error, unexpected end-of-input, expecting keyword_end i += 2 ^
A while
-loop can accomplish all looping tasks. But other constructs, called iterators, are also available. They improve the syntax for many loops, and they reduce errors.
while
-loop.Programs handle not just single units of data, but ranges, collections of data. In loops we handle this data. Nearly every program will have loops.