Development Bootcamp – Day 3: Loops & Iterators

The wheels on the bus go round and round – Just like our loops and iterators today!

The day started of a bit hectic as the metro had some technical problems. Today it took me 90 minutes instead of 45 to get to WeWork. So I arrived late and pretty annoyed about public transport once again. But there was no time to waste I had some catching up to do.

It all started pretty simple with a regular loop. A loop is used to do something repeatedly. The only danger when creating a loop is repeating something infinitely. In order to avoid an infinite loop, conditions have to be added to the loop.

### Simple loop ###
loop do
    puts "This loop will print this string infinitely!"
end

### Loop with conditions ###
i = 0
loop do
  i += 1
  if i == 3
    next        # skips "This is loop 3."
  end
  puts "This is loop #{i}."
  if i == 5
    break       # this will exit the loop
  end
end

After the regular loop we went on with the while and for loop.

A while loop checks a parameter that evaluates to either true or false. The code within the while loop executes when the parameter evaluates to true. Once the parameter evaluates to false the while loop is not executed again, and the program continues after the while loop.

### While loop ###
i = 0
while i < 5
  i += 1
  puts "In the loop #{i} time(s). I will stop after 5 times."
end

In Ruby, for loops are used to loop over a collection of elements. Capturing a range in Ruby is done by using a special type. A for loop can also be used to iterate over the values in an array.

1..5 is a range including the 5
1...5 is a range excluding the 5

### For loop using a range ###
for i in 1...6
  puts "Push-up count: #{i} ."
end

### For loop using an array ###
pizzas = ["Margherita", "Funghi", "Tonno", "Salami"]

for pizza in pizzas
  puts "I would like to order a pizza #{pizza}."
end
    

We got back to arrays and hashes which we were introduced to yesterday. We learned how to call methods like .last and .first on an array. (Getting the first or last value in an array). And how to modify arrays by using the .push and .pop methods. ( Adding to or deleting values from the array).

Last but not least we learned about a special thing within Ruby called the each iterator. This smart method returns all the elements of a collection, one after the other.

The each iterator is always associated with a block. It returns each value of the array, one by one, to the block. The value is stored in the variable you assign between the pipes || and can then be displayed on the screen.

### Array of strings ###
pizzas = ["Margherita", "Funghi", "Tonno", "Salami"]

### Each iterator ###
pizzas.each { |pizza| puts "I want a pizza #{pizza}"}

After every new block of information we had to complete several exercises to practice the newly gathered information. Finally, at the of the day we had to try to create some bigger programs/exercises. One of these programs was a "Guess the number game", you can see my version in action below:

numbers_game

### Guess the number game ###

number = rand(100)
i = 0
puts "Welcome to the number game, guess a number between 0-100 in 10 tries."
loop do
    puts "Make a guess?"
    i +=1
    if i == 10
        puts "Game over!! You tried 10 times!"
        break
    end
    guess = gets.chomp.to_i
    if number > guess 
        puts "Higher!"
        next
    elsif number < guess
        puts "Lower!"
        next
    else number == guess
        puts "You win!!"
        break
    end
end

Over and out, until tomorrow!

Leave a reply:

Your email address will not be published.