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 Ruby Loops Ruby Loops The While Loop

Problem with While Loop Code

Hi,

Not sure what's wrong with my loop code. Any ideas?

i = 0 def i_loop(i) while i < 5 i = i + 1 end end i_loop(i)

This also seems like it should work but doesn't:

def i_loop() i = 0 while i < 5 i = i + 1 end end

i_loop()

1 Answer

The loop seems to be working on my end.

def i_loop() 
    i = 0 
    while i < 5 
        i = i + 1 
    end 
end

i_loop

You may not see anything because you're not printing or returning anything. If we added a puts statement to print out i at every iteration, you would be able to see the loop in action.

def i_loop() 
    i = 0 
    while i < 5 
        i += 1 
        puts i
    end 
end

i_loop