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
Anthony Ross
2,756 PointsUnderstanding the "Until" loop
I'm working with the following bit of Ruby code:
name = ""
until name == "Jason" do
name = gets.chomp
puts "Your name is: #{name}"
end
I understand that this loop will keep going until it's given "Jason". What I don't understand is why when I type "Jason", I still see the puts "Your name is: Jason" and then the program ends.
I'm wondering why the 'puts' method was used here, when one would think the loop wouldn't even run now that it's been given the value of "Jason" (so the next 2 lines wouldn't be used).
What am I not understanding?
3 Answers
James Barnett
39,199 PointsMaybe this flowchart will help
Andre' Jones
26,671 Pointsthe loop is will always cycle through at least once as long as ur name is set to an empty string as you have done or something other than Jason. when ur trying to understand loops or any other methods just take some example inputs and go down the line and see what u get.
Anthony Ross
2,756 PointsHi James - That flowchart seems to prove my point. if name == Jason it jumps to "End". It doesn't get to "read name" or to "print name", so if I enter Jason the program should just end, not "Your name is Jason".
Andre' I'm not sure I understand what you're outlining.

James Barnett
39,199 PointsJames Barnett
39,199 Pointsnameis initalized to nullnameis set to "Jason"nullcontinue into theuntilblocknameequal to Jamesnamewhich is currently "James"nameis set to "Jason"namewas set to "James" in step 3-3 continue into the until blocknameequal to Jasonnamewhich is currently "Jason"nameis set to "Jason"Jasonin step 5-3 don't execute theuntilblockAnthony Ross
2,756 PointsAnthony Ross
2,756 PointsOh, my duh! YES! Sigh, thank you sir - that makes total sense!