# FAQ: Loops & Iterators - Next!

This community-built FAQ covers the “Next!” exercise from the lesson “Loops & Iterators”.

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

## FAQs on the exercise Next!

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.

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

Need broader help or resources? Head here.

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

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!

Hey there, I would like to find out how to find out how to skip next iteration if the number `i` is odd.
the code is
i = 20
loop do i -= 1
next if ____________
print “#{i}”
break if i <= 0
end

I think this is what you might be trying to do:

``````i = 20
loop do
i -= 1
next if i % 2 != 0
puts "#{i}"
break if i <= 0
end
``````

Output:

18
16
14

2
0

Here’s a shorter way if you just want to print every other value:

``````j = 20
while j > 0
puts j
j -= 2 #decrement by 2 to skip every other value
end
``````

Same output.
Hope this helps!

4 Likes

Put in the conditional expression…

``````i % 2 != 0
``````
2 Likes

thank you so much! that was very helpful !

1 Like

I am sure this post should be located somewhere else, but the link directed me here from the course. When completing this exercise that the OP described, I originally used basically the same code recommended in the responses. That is, simply adding in the check for an odd number. However, when I used ‘puts “#{i}”’ after the next statement, the exercise failed with a message like “it appears your loop does not print out the numbers from 18 to 0”. Obviously, this seems like a bug in the program that checks answers for correctness, since while reading that message I was viewing the numbers 18 down to 0, without odds. For some reason, this exercise will only accept the answer as correct if a print statement is used, which I do not like because then you have all the numbers printed right after each other on one line horizontally. Anyway, just thought I should mention this in case someone has trouble with it. Hopefully a team member or important person will eventually read this and fix the issue in the exercise. Thanks for anyone willing to help out or tell me why I am wrong (which is a much more likely case…).

4 Likes

You could also use the ‘odd?’ method

``````i = 20
loop do
i -= 1
next if i.odd?
print "#{i} "

break if i <= 0
end
``````
2 Likes

I had a similar issue to @whcherry52 - but I had forgotten that in a previous step I had “improved” the output to have spaces between the numbers.
My code output `18 16 14 12 10 8 6 4 2 0`, but it is looking for `181614121086420`

3 Likes

snap! I did the same - I added a space for legibility!

thanks for this I literally had no idea what i did wrong!!

Hello! I am really confused by this line :
next if i % 2 != 0
If 19 % 2 returns 1, than 1 !=0 is true… why it doesn’t write odd numbers but even…

Hello, @array6418683729.

Welcome to the forums. Assuming your referring to `next if i % 2 != 0` being included inside a loop, `next` will skip the remaining code in the loop, and move on to the next iteration when the `if` condition evaluates to `truthy`.
The code below for example will only print the even values of `i`. When the `if` condition evaluates to `truthy`, the `puts` statement is skipped, and the ‘next’ value of `i` is evaluated.

``````for i in 1...20
next if i % 2 != 0 #if i is odd, the remaining code is skipped, and the next iteration of the loop occurs
puts i
end
``````

Output:

2
4
6
8
10
12
14
16
18

Also odd numbers leave a remainder of one when divided by two so I went down the following route:

``````next if i % 2 == 1
``````

I hadn’t thought of the ‘if it isn’t zero’ route though, so thanks.

2 Likes

Seems to be the same issue as @apex described still within this challenge. Disappointed to see it still hasn’t been fixed. Code is checking for the string 181614121086420 exactly, so if you replace “print” with “puts” it will fail to notice you solved it successfully.

Bit suprising given that I haven’t had issues with any of the other challenges when making small changes like this; wonder why its just this one that seems to have been programmed odd?

1 Like

Hi! Why should we use `print "#{i}"` instead of `print i` here? if you remove “#{}” the output is the same.

What if `i` is used in a sentence, interpolated?

Why do you add an exclamation point??

For this exercise the output expected is,

``````181614121086420
``````
``````!=
``````

is the NOT EQUAL TO operator.

thank you so much for posting this. i had also added the space for readability, and despite my answer being correct (next if i % 2 >= 1 ) it was still telling me it was wrong.

i’m not really sure i understood the logic in this exercise or how the iteration works i literally entered the same code as you by chance and it worked. i feel like it would have benefitted from being a little longer to explain the iteration process in a little more depth…