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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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!
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?
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.
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
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’.
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.