FAQ: Loops & Iterators - Looping with 'Until'

This community-built FAQ covers the “Looping with ‘Until’” exercise from the lesson “Loops & Iterators”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Looping with ‘Until’

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

When I write out the code for the ‘until’ loop and I run the following code

i = 0
until i >= 50 do
print i
i += 1
end

it prints 1 - 49. Any idea why?

1 Like

Not to be too obvious, but that’s exactly what you told the program to do. You start with i = 0 then tell the computer to print i until i >= 50. You are adding 1 to i after each print i statement, so it prints the value of i until i gets to 50. If you wanted to include the number 50, just leave the = off of your until condition. Then it will print i until i > 50. Hope this helps!

1 Like

yes it does! Thank you so much!

Hi.

I have been trying to use this solution you gave as a model for the solution to the Instructions for Lesson 5 Section 15 but I just can’t seem to figure it out. I think I’m confused about the range from 0-50 for the j = 3 section . Every time I try a new code using < 50 or 1…50 I end up in an endless loop.

i = 3
while i > 0 do
print i
i -= 1
end

j = 3
until j == 0 do
print j
j -= 1
end

Now rewrite your while loop using until. You still want to print out the numbers 1 through 50, inclusive.

This is one of my many attempts which is wrong:

j = 3
until j ==0 < 50 do
print j
j -= 1
end

Thanks for any explanation you can give me.

Hi Midlidner,

I have a question for you but I incorrectly only sent it to bit4005433864. I would appreciate if you could please take a look at it as well.

Here it is:

Version:1.0 StartHTML:000000243 EndHTML:000040024 StartFragment:000009080 EndFragment:000039976 StartSelection:000009660 EndSelection:000039929 SourceURL:FAQ: Loops & Iterators - Looping with 'Until' FAQ: Loops & Iterators - Looping with ‘Until’ - Guide / Ruby FAQ - Codecademy Forums {"@context":“http://schema.org","@type”:“WebSite”,“url”:“https://discuss.codecademy.com",“potentialAction”:{"@type”:“SearchAction”,“target”:“https://discuss.codecademy.com/search?q={search_term_string}",“query-input”:"required name=search_term_string”}} {"@context":“http://schema.org","@type”:“QAPage”,“name”:“FAQ: Loops & Iterators - Looping with ‘Until’”,“mainEntity”:{"@type":“Question”,“name”:“FAQ: Loops & Iterators - Looping with ‘Until’”,“text”:"<a class=“lightbox” href="//codecademy-discourse.s3.dualstack.us-east-1.amazonaws.com/original/5X/d/8/8/1/d88176b1e3cf3b7c8c9b1ac4a094dd5d21447876.jpeg" title=“Community FAQs on Codecademy Exercises.jpeg” rel=“nofollow noopener”>[Community%20FAQs%20on%20Codecademy%20Exercises]</a>\nThis community-built FAQ covers the “Looping with ‘Until’” exercise from the lesson “Loops & Iterators”. \nPaths and Courses \nThis exercise can be found in the following Codecademy content: \n<a href=“http://codecademy.com/courses/learn-ruby/lessons/loops-iterators/exercises/looping-with-until” rel=“nofollow noopener”>Learn Ruby</a> \nFAQs on the exercise Looping with ‘Until’\n\n\nThe…",“upvoteCount”:0,“answerCount”:0,“dateCreated”:“2018-10-26T00:04:49.923Z”,“author”:{"@type":“Person”,“name”:“Wiki Bot”}}} #reply-control .d-editor-preview img:not(.thumbnail), .cooked img:not(.thumbnail) {max-width:690px;max-height:500px;} /* Copyright 2014 Evernote Corporation. All rights reserved. */ .en-markup-crop-options { top: 18px !important; left: 50% !important; margin-left: -100px !important; width: 200px !important; border: 2px rgba(255,255,255,.38) solid !important; border-radius: 4px !important; } .en-markup-crop-options div div:first-of-type { margin-left: 0px !important; }

Hi.

I have been trying to use this solution you gave as a model for the solution to the Instructions for Lesson 5 Section 15 but I just can’t seem to figure it out. I think I’m confused about the range from 0-50 for the j = 3 section . Every time I try a new code using < 50 or 1…50 I end up in an endless loop.

i = 3
while i > 0 do
print i
i -= 1
end

j = 3
until j == 0 do
print j
j -= 1
end

Now rewrite your while loop using until . You still want to print out the numbers 1 through 50, inclusive.

This is one of my many attempts which is wrong:

j = 3
until j ==0 < 50 do
print j
j -= 1
end

Thanks for any explanation you can give me.

Reply

Bookmark Share Flag Reply

Unpinned

This topic is unpinned for you; it will display in regular order

Tracking

You will see a count of new replies because you posted a reply to this topic.

Suggested Topics

Topic Replies Views Activity
FAQ: Getting Started - Generalizations

Ruby FAQ|1|163|Feb 2|
|FAQ: Introduction to Ruby - Multi-Line Comments

Ruby FAQ|2|201|Nov '18|
|FAQ: The Zen of Ruby - Conditional Assignment

Ruby FAQ|0|135|Nov '18|
|FAQ: Data Structures - Iterating Over Arrays

Ruby FAQ|0|133|Nov '18|
|FAQ: Data Structures - Creating Arrays

Ruby FAQ|0|155|Nov '18|

There are 31 new topics remaining, or browse other topics in Ruby FAQ

[en_US.share.topic]

New Topic

Quote

Invalid dateInvalid date

midlindner

Hi Midlidner,

I have a question for you but I incorrectly only sent it to bit4005433864. I would appreciate if you could please take a look at it.

Hi.

I have been trying to use this solution you gave as a model for the solution to the Instructions for Lesson 5 Section 15 but I just can’t seem to figure it out. I think I’m confused about the range from 0-50 for the j = 3 section . Every time I try a new code using < 50 or 1…50 I end up in an endless loop.

i = 3
while i > 0 do
print i
i -= 1
end

j = 3
until j == 0 do
print j
j -= 1
end

Now rewrite your while loop using until. You still want to print out the numbers 1 through 50, inclusive.

This is one of my many attempts which is wrong:

j = 3
until j ==0 < 50 do
print j
j -= 1
end

Thanks for any explanation you can give me.

Ok. Looks like you just need an until loop that will print the numbers 1 through 50. First we need a variable to increment through those numbers. Since we want to start with 1 we may as well initialize the variable that way: j = 1
Now we need an until loop with a condition that will stop after we print the number 50. We can use: until j > 50 do Now we’re in the clear! We print j and then add 1 to j inside the until loop’s code block, and we have what we wanted. Hopefully this helps. I’ve included the solution below if you get stuck.

j = 1
until j > 50 do
  print j
  j += 1
end

Happy Coding!

2 Likes

Thank you very much!

1 Like

numbers.each { |element| puts element } with the use of the .each, what ist the point of a placeholder, wouldn’t it be easier to have numbers.each { puts}, removing the placeholder and just tipping out the required action?