14. Looping with 'While'

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
My link to 14. Looping with ‘While’

<In what way does your code behave incorrectly? Include ALL error messages.>
This is what I get.
(ruby):4: syntax error, unexpected $end, expecting keyword_end

```

i = for num in 1…50
while i < 50 do
print i
i += 1
end

<do not remove the three backticks above>

Can you help me? Thanks!

Hey there, got pretty stuck on this too. firstly define I as just 1, so you get a starting point.
In your while section do I < 51, so you go upto but not over, 50.
The rest looks good to me. Here is the overall loop:

i = 1
while i < 51 do
print i
i += 1
end

3 Likes

Can you perhaps explain why start with 1 and not 0 ?

@aleafonthewind [quote=“aleafonthewind, post:3, topic:43031, full:true”]
Can you perhaps explain why start with 1 and not 0 ?
[/quote]

You start with 1 and not 0 because you have to print from 1 to 50, inclusive, so if you start with 1, it will be easier.

2 Likes

High there. Can someone explain to me why you set i to 1 and increment it up to 50? also, why does using an inclusive range (1 … 50) not work?

i = 1
while i < 51 do
    print "#{i} \n"
    i += 1
end

why did this not work? Says didn’t print 1 to 50 inclusive
also tried printing reverse to see if it worked, but still no

Hi there try this

i = 1
while i < 51 do
print “#{i}”
i += 1
end

What was different from my code?

This should work

i = 1
while i < 51 do
    print i
    i += 1
end

this work

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

1 Like