Key Concepts

Review core concepts you need to learn to master this subject

Ruby while Loop

Putting a block of code in a while loop in Ruby will cause the code to repeatedly run the code as long as its condition is true.

If the block of code doesn’t have a way for the condition to be changed to false, the while loop will continue forever and cause an error.

Loops & Iterators
Lesson 1 of 2
  1. 1
    Sometimes you want to repeat an action in Ruby while a certain condition is true, but you don’t know how many times you’ll have to repeat that action. A good example would be prompting a user for a…
  2. 2
    Did you see that? The loop printed out the numbers 1 to 10, then stopped. This was because the loop’s condition said to continue while counter was less than 11; since counter went up by 1 each time…
  3. 3
    The complement to the while loop is the until loop. It’s sort of like a backward while: i = 0 until i == 6 i = i + 1 end puts i 1. In the example above, we first create a variable i and set it…
  4. 4
    We’ve been using syntax like counter = counter + 1, which works, but as you’ll increasingly find with Ruby, there’s always another way. A shortcut is to use an assignment operator. You already k…
  5. 5
    Sometimes you do know how many times you’ll be looping, however, and when that’s the case, you’ll want to use a for loop.
  6. 6
    You saw a bit of new syntax in the previous exercise: for num in 1…10. What this says to Ruby is: “For the variable num in the range 1 to 10, do the following.” The following was to puts “#{num}”…
  7. 7
    Good work! You’re ready to build your very own for loop.
  8. 8
    So far we’ve learned one way to repeat an action in Ruby: using loops. As is often the case in Ruby, however, there’s more than one way to accomplish a given task. In this case, it’s also possible …
  9. 9
    The next keyword can be used to skip over certain steps in the loop. For instance, if we don’t want to print out the even numbers, we can write: for i in 1..5 next if i % 2 == 0 print i end …
  10. 10
    Let’s say we want to save a range of numbers in a variable. How would we do this? A variable can only hold a single value, right? In Ruby, we can pack multiple values into a single variable using …
  11. 11
    Great work! You’re really getting the hang of this. The loop iterator is the simplest, but also one of the least powerful. A more useful iterator is the .each method, which can apply an expression…
  12. 12
    Cool, no? Now it’s your turn to take the .each method for a test drive. numbers = [1, 2, 3, 4, 5] # one way to loop numbers.each { |item| puts item } # another way to loop numbers.each do |item|…
  13. 13
    The .times method is like a super compact for loop: it can perform a task on each item in an object a specified number of times. For example, if we wanted to print out “Chunky bacon!” ten times, w…
  14. 14
    Okay, training wheels off. Let’s see your stuff! i = 3 while i > 0 do print i i -= 1 end 1. In the above example, we create a variable called i and set it to 3. 2. Then, we print out 321 sin…
  15. 15
    Good work! i = 3 while i > 0 do print i i -= 1 end j = 3 until j == 0 do print j j -= 1 end In the example above, we wrote the same loop using while and using until.
  16. 16
    In case you’re not picking up on the theme of Ruby having a gajillion ways to do any given task: let’s convert our loop yet again. for k in 1..3 print k end In the above example, we print out …
  17. 17
    Great work! We’ll give you a bit of a break from the numbers game. m = 0 loop do m += 1 print m break if m == 10 end In the example above, we print out 12345678910 since we loop 10 times.
  18. 18
    Finally, let’s replace our loop with the .times iterator.