Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

loops

I want to create a loop that will repeat until it has printed the number of strings I want it to, ie. I want to tell the computer to say "moo" as many times as I want, like 10.

3 Answers

Robert Komaromi
Robert Komaromi
11,927 Points

Using your example of printing the string "moo" 10 times, there are multiple ways to do this:

for i in 1..10
    puts "moo"
end

i = 0
until i == 10
    puts "moo"
    i += 1
end

i = 0
while i < 10
    puts "moo"
    i += 1
end

(1..10).each do
    puts "moo"
end

for i in 1..10
    puts "moo"
end

i = 0
loop do
    puts "moo"  
    i += 1
    break if i == 10
end
Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

What do I do if I want to exit with a number and not print it?

Robert Komaromi
Robert Komaromi
11,927 Points

To break out of a loop, use the break keyword. Example:

for i in 1..10
    puts "moo"

    # This will stop the loop right after
    # moo has been printed out the 5th time
    break if i == 5
end

You don't have to print anything inside the loop, and in the example above, you can access the variable i after the loop has completed. Does this answer your question? What do you mean by "exit with a number"?

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

I want to setup my loop so that if I enter a number larger then 1,000,000,000,000,000,000,000,000,000 (don't ask me what that number is, because I don't know) it will break, but when I use the following code it prints and then breaks, I want it to break and not print.

def print_hello(number_of_times)
  i = 0
  while i < number_of_times
    puts "moo"
    i += 1
   end
  end

answer = 0
while answer < 1000000000000000000000000000
  print "How many times do you want me to say moo? (If you want to exit enter a number bigger then 1,000,000,000,000,000,000,000,000,000): "
  answer = gets.chomp.to_i
  print_hello(answer)
 end
Robert Komaromi
Robert Komaromi
11,927 Points

For you to understand your problem, you have to understand how while loops work. You can read about while loops in the Ruby docs here: http://ruby-doc.org....

The condition is checked first, then the code inside the loop runs, then condition is checked again. Do you see why print_hello(answer) is executed once even if the user enters a number greater than 10^27?