Ruby Ruby Loops Ruby Loops The While Loop

Carl Smith
Carl Smith
8,085 Points

Can't figure out this while loop

Note sure what I'm missing?

loop.rb
i = 0
while i > 5
  i += 1
end

2 Answers

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

You just got the less than and greater than sign mixed up a bit! Try this:

i = 0
while i < 5
  i += 1
end
Carl Smith
Carl Smith
8,085 Points

Thank you! The little things get me every time!

Michael Hulet
MOD
Michael Hulet
Treehouse Moderator 46,973 Points

This loop will never run. You initially set i to 0, but the loop will only run if i is greater than 5. My bet is that you meant less than. This will fix that:

i = 0
# Notice the reversed comparison sign
while i < 5
  i += 1
end
Carl Smith
Carl Smith
8,085 Points

Thank you! The little things get me every time!

Jennifer Nordell
Jennifer Nordell
Treehouse Staff

To be fair, it's rarely the giant logic flaws that get us, is it? It's always the misspelled variable or missing quotation mark, etc! That's why it's so nice that people hang out here on the forums. Some days it's nice just to have an extra set of eyes! :)