# Introduction To Ruby

Upto & Downto

The **upto** and **downto** methods are another way we can use iteration in our Ruby programs. Again, using them for the kinds of program we have been writing so far, can lead to more readable and more natural code.

## Example 1 - upto

`# upto`

1.upto(10) {|i| puts i}

gets

Notice the use of curly braces to enclose the block. The pipe character is used to specify our iterator variable, **|i|**. We had the choice to do this with some of our other iteration structures. It's useful when there is only one statement per iteration.

## Example 2 - upto

This example calculates the square of each of the numbers and uses a longer block.

`# upto 2`

1.upto(10) do |i|

i_squared = i**2

puts "#{i}: #{i_squared}"

end

gets

## Example 3 - downto

We could get while or until loops to count backwards. Using the **downto** method is a little more natural though if we are counting down in ones.

`# downto`

10.downto(-10) do |i|

puts i

end

gets

## Challenges

Use the **upto** or **downto** methods for these challenges.

- A formula for the n
^{th}term of each sequence is given below. For each sequence, write a program that writes out the first 10 terms of each of the following sequences- n+2
- 20-n
- 5n+4 (5 times n + 4)
- 0.5n+1
- n
^{2} - n
^{3} - n
^{2}+2n+1

- Find the total of the numbers from 1 - 100 that are exactly divisibly by both 2 and 3.
- Find the total of the odd numbers from 1 - 100.
- Find the total of the even numbers from 1 - 100.
- Find the total of the even numbers from 1 - 1000 that are exactly divisible by 3.
- Find the total of the odd numbers from 1 - 1000 that are exactly divisible by 7.