FAQ: Loops & Iterators - Looping with 'While'

This community-built FAQ covers the “Looping with ‘While’” 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 ‘While’

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!

Hi. This is from Lesson 5: Loops & Iterators Section 14 Looping with “while”.

This is the code that was given:
i = 3
while i > 0 do
print i
i -= 1
end

  1. In the above example, we create a variable called i and set it to 3 .

  2. Then, we print out 321 since we execute the loop as long as i is positive.

    I would greatly appreciate if someone could explain this to me as I don’t understand where the 321 comes into play.

Thank you very much!

1 Like

The 2 lines of code between while and end will be repeated until the condition specified in the while statement is no longer true. print i prints the value of i and then i -= 1 subtracts 1 from the value of i, so the first iteration of the while loop prints 3 (the initial value of i), the second iteration prints 2 ( i -= 1 changed i to 2), etc. Since we’re using print without adding any spaces or new lines, the output is 321 Hope this helps!

2 Likes

Thank you very, very much for your easy to understand explanation.
Now, I can finally go to bed!

1 Like

I thought { and } could replace do and end respectively. Is this not the case? why do I get this?

1 Like

Hello, @cloudrockstar46685. Welcome to the forum!
You can’t use curly braces { } with a while loop in Ruby.
You can omit the do:

n = 1
while n <= 50
  print n
  n += 1
end

You may find this topic interesting: https://stackoverflow.com/questions/35726738/how-to-do-a-one-liner-while-loop-in-ruby-using-curly-braces
Happy coding!

3 Likes

Why doesn’t running this code cause the checklist to verify that I’ve completed the assignment? Is it because the code is going down from 50 instead of up from 1? Also, why does changing “i = 50” to “i <= 50” fix the problem by reversing the order?

Hello, @data8822600657.

Welcome to the forums.

The exercise specifically asks us to count up, so yes, that is why you don’t receive the check mark for passing.

Not sure I follow you here. Changing only the first line of your code from i = 50 to i <= 50, and leaving your remaining code as is should throw an error. i <= 50 is a comparison not an assignment, so you will get an undefined error. To count up rather than down you would need to assign i a value of 0 initially, and then in your while statement you would use i <= 50, and inside the body of the while loop you would increase i by 1 each time rather than decrease.

1 Like

I have apparently completed the task satisfactorily, but I’m getting a lot of warning messages in between the print results.

U need to space between your OneToFifty and =, It should looks like this

OneToFifty = 1
while OneToFifty < 51
print OneToFifty
OneToFifty += 1
end

Under “Loops and Iterators. Looping with while”, this works:

i = 0
while i <= 50 do
 print i
 i+= 1
end

however, this doesn’t:

i = 0
while i <= 50 {
 print i
 i+= 1
}
 

Why not? A bit earlier in the chapter it is stated that curly brackets and do/end are interchangeable. Yet the code only works when using the do/end keywords.

This is the error I get:

(ruby):1: syntax error, unexpected '{', expecting keyword_do_cond or ';' or '\n'
(ruby):4: syntax error, unexpected '}', expecting end-of-input

Not sure if block syntax works when there are line breaks.

arr.each { x | puts x }

For multiple lines we need to use do and end (or some other construct) as I understand it.

When the lesson first introduced While Loops it didn’t use ‘do’ in the code. But in this exercise it has. Is there a reason? It seems to work without ‘do’.

old code without do:

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

new code with do:

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

Both give the same result.

The second example is exactly the same as the first, given that do is optional in that syntax, meaning it is implied when absent. do, itself is the opening of a code block.

Note that if the code is written as,

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

it is not the same loop since this one will run at least once. Pretty sure I read once, a long time ago, that this code is not recommended, as in discouraged for the simple reason that it is not the same as the opening examples, which can lead to confusion on the part of the reader. If the syntax is still supported, one may wish to do some further research on the point.

2 Likes