FAQ: Learn Python: Loops - Continue

This community-built FAQ covers the “Continue” exercise from the lesson “Learn Python: Loops”.

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

Join the Discussion. Help a fellow learner on their journey.

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

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!

5 posts were split to a new topic: What is the scope of a variable that is part of a for loop?

So I don’t get this, surely it would be much more efficient to just do

for i in ages:
if i >= 21:
print(i)

3 Likes

But if you did that you wouldn’t learn about the existence continue

But why isn’t the else statement needed here:

for i in ages:
if i < 21:
continue
print(i)

?

my code looked like this:

ages = [12, 38, 34, 26, 21, 19, 67, 41, 17]
for age in ages:
if age < 21:
continue
print(age)

and it printed: 17

obviously when I indented it correctly I got the right answer but I can’t imagine why it would have printed 17 in this case.

edit: oh dear it doesn’t let me save the indents. basically I had everything indented correctly except the print statement was on the same level as the word for

4 Likes

i dont get it…

shouldnt it be printing all the ages that are more then 21?
becuse age > 21…?

The print statement is never reached when age > 21. Remove `continue` and indent the print statement.

1 Like

ok but why dose it do the opposite if the statement dont reached?

Because the way it is written above the print statement is not in the if statement.

I’m just curious.

In the example:

numbers = [1, -2, 3, -4, 5, -6, 7, 8, -9, 10]

for p in numbers:

if p >= 0:

``````continue
``````

print(p)

Why does print(p) action print out negative integers. Once the if statement is set to greater than or equal to 0.

Because it is preceded by `continue` in the condition, which means the print statement is skipped over except when the numbers are negative.

You certainly could use an else statement, but it is unnecessary, the for loop will just move onto the next line. If statements in general do not require else statement to run

Hi anyone!

Just out of curiosity, I tried to use “continue” in a while loop but ended up creating an infinite loop. Below is the code I tried:

``````ages = [12, 38, 34, 26, 21, 19, 67, 41, 17]

# for age in ages:
#   if age < 21:
#     continue
#   print(age)

index = 0
while index < len(ages):
if ages[index] < 21:
continue
print(ages[index])
index += 1
``````

Does anyone have an idea why I’m hitting infinite loop

The variable `index` never changes value.

1 Like

ooh gotcha. I just realized that “continue” will keep checking ages[0] if it’s < 21. So maybe a better way would be to increment index before hitting “continue” like so:

``````index = 0
while index < len(ages):
if ages[index] < 21:
index += 1
continue
print(ages[index])
index += 1

``````

this way works. We probably would do the same for “for loop” if we are accessing the elements by the index.

Thanks @mtf

1 Like

ages = [12, 38, 34, 26, 21, 19, 67, 41, 17]

``````for eligible in ages:

if eligible < 21:

continue

print(eligible)
``````

I have a question about the indentation of the print function. why does it have to be outside the if statement to work? Sorry if this seems elementary but I can’t wrap my head around it.

Read up on the importance of indentation in identifying code blocks. In the above code we don’t want it to print inside the if statement, do we?

I recognize that but the question is why? shouldn’t it also loop?

To reiterate, check what block the statement is in. Make your indentations easy to see and deliberate so they can do their job.

``````for ...:
if ...:
# code
print (...)``````