Now you are going to be introduced to one of the nifty aspects of computer programming: loops.

Using your favorite text editor, type the following:

codetitle. loops.rb

4.times do
  puts "Hello World"
end

Can you guess what this piece of code does? Save the file as loops.rb and run it:

shell> ruby loops.rb
Hello World
Hello World
Hello World
Hello World

As you can see, the contents of the loop have been executed 4 times. This is the most straight forward loop that Ruby offers.

Counting

Here is another example. Now we use our knowledge of variables to print the numbers from 1 to 5.

codetitle. counting.rb

count = 0
5.times do
  count += 1
  puts "count = " + count.to_s
end

Remember that the Integer#to_s method converts the integer into a string, so we can add it to the string "count = ".

When you run this you should get:

shell> ruby counting.rb
count = 1
count = 2
count = 3
count = 4
count = 5

A sum of numbers

Suppose that I want to know the sum of all the numbers from 1 to 11. We already know how to get all the numbers from 1 to 11. All we really need to do is add them together:

codetitle. sum.rb

count = sum = 0
7.times do
  count += 1
  sum   += count
  puts "sum at " + count.to_s + " = " + sum.to_s
end

You should get something like this:

shell> ruby sum.rb
sum at 1 = 1
sum at 2 = 3
sum at 3 = 6
sum at 4 = 10
sum at 5 = 15
sum at 6 = 21
sum at 7 = 28

Multi-line statements

That puts statement in the last example was getting somewhat long. What happens if you want to type a very long line?

You can make lines “wrap around” by putting a backslash \ at the very end of the line. Look at this example in irb.

shell> irb --simple-prompt
>> puts "Hello " + \
?>      "world"
Hello world
=> nil
>>

That ‘=> nil’ simply means that puts returns nothing. In other words, if you typed:

variable = puts "hello"

Then variable would end up with nothing. And Ruby’s object for “nothing” is nil.

What you should be paying attention to is the fact that we spread out the puts over two lines. Let’s use what we just learned to rewrite that line in our program:

codetitle. sum.rb

count = sum = 0
7.times do
  count += 1
  sum   += count
  puts "sum at " + count.to_s \
     + " = "     + sum.to_s
end

You don’t have to make the program line up like that. I did it because I think it looks better.

Warning: The backslash ‘\’ must be the last character in the line. If you put so much as a space after it, you will get an error.

More examples

Counting backwards

Type in this program:

codetitle. countdown.rb

count = 10
10.times do 
  count -= 1
  puts count
end
puts "Lift off!!"

This produces:

shell> ruby countdown.rb
9
8
7
6
5
4
3
2
1
0
Lift off!!

Counting a variable number of times

These loops also work with variables.

In this example, we compute the factorial of a number. The factorial of a number n is the product:

1 × 2 × 3 x … x n

The symbol for this is n!. In this example we compute.

6! = 6 × 5 × 4 x 3 × 2 × 1 = 720

Type and run this program:

codetitle. factorial.rb

number = 6
count = 0
product = 1

number.times do
  count   += 1
  product *= count
end
puts number.to_s + "! = " + product.to_s
shell> ruby factorial.rb
6! = 720

Exercises:

Loopy Presidential Candidates: A tribute to Super Tuesday!