14. Looping with 'While'


#1



https://www.codecademy.com/en/courses/ruby-beginner-en-XYcN1/3/1?curriculum_id=5059f8619189a5000201fbcb

Hi everyone, having some trouble with this code. To me it makes sense because I assigned the number 1 to x and I said that while x is bigger than 0 (not including 0 therefore should start from 1) do:

  • print x (i tried both interpolation and without it but still do not work)
  • increment it by 1
  • break the loop if x is less than or equal to 50 (so should include 50 as well)

The error message was 'Oops, try again. It looks like you didn't print out the numbers 1 to 50, inclusive.'
Also the console only prints '1nil'
I had a look at other answers but I want to use the while loop as instructed.

Is this not working because I can't say x is bigger than and then say that when it is smaller than at the break point?


x = 1
while x > 0 do
    print "#{x}"
    x += 1
    break if x <= 50
end


#2

HI they want you to use only use the while statement without the break if and you should loop from 1 to 50 so you should change this condition

while x > 0

because x (x will always be great than 0) and it will cause you an infinit loop

#first loop
1 > 0
#second loop
2 > 0
..

#3

Oh right I see! I was complicating things! So without using break if it would be this:

x = 1
while x <= 50 do
  print x
  x += 1
end

#4

So without using break if it would be this:
-Yes


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.