FAQ: Learn Python: Loops - Continue

I have the exact same formatting and layout as the solution with zero differences and only get
17.

When I replace the code with the solution (which is identical) I get the ‘right answer’
Is this broken ???

the indents might be wrong!

excuse me i did all the work on continue, but can you please solve this problem?

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

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

Great pun!
image

Is the way the lesson has us do it more efficient than:

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

for age in ages:
if age > 21:
print(age)
else:
continue
I think it’s more understandable to check for the positive condition (where we want to print) than the inverse. If the goal is to be understandable, then I think this is better. But I am not sure if the else statement will slow down processing.

That is redundant. Your loop will run fine with that removed. Remember to include 21 in your condition.

>= 21

weird way of doing that, ‘continue’? just curious, why do you think they(the developers of the language) chose the word ‘continue’ to do what it is doing?

Let’s look at a simple example:

a = range(10)
for x in a:
    if x % 2:
        continue
    print (x)
0
2
4
6
8

Yes, we could configure our range to do the same thing but think in dynamic terms where we do not control the data and it may even be random in nature. It doesn’t even have to be numeric. The type of controlled loop above could have many applications.

The main purpose of the instruction is to drop the current iteration cycle and commence a new one. The loop itself will have a chance to run completely through while skipping some of the iterations based on the conditional we set forth.

i know that, i was asking your thoughts/opinions/theory on the reasoning behind using the word ‘continue’ to skip the current iteration

TBH I have no thoughts, opinions or theories with regard to continue. We have only to know what is promised and then decide on a case by case basis whether it fits our logic, or not. The example above may be over simplified but there is no real reason to complicate things. We know what the instruction does, and have only to apply it, or alternate logic in our controlled loops.

Note above that we could follow the continue by an entire block of code, not just a print statement. Of course our choice of a controlled loop would be notwithstanding since continue only works in for loops, as far as I know. Will need to do some more reading on it.

As mentioned earlier, I have what I need, the promise made by the language interpreter. It does not matter what I think of that since it won’t change anything. Some things just don’t need to be considered beyond learning what is promised. Need we think on this any further?

either you misinterpreted something, or maybe i, or maybe i should just drop this. sure, the topic can be dropped i will leave you alone, sorry if i wasted your time.

I see there is a lot of questions here already, and this might already have been asked, but I feel like i have to ask because I don’t fully understand.

Why does this work:

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

While this does not work:

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

The first one includes 21, even though (if I understand it correctly), it checks for ages above, and not included, 21.
The second one excludes 21, despite checking for numbers equal to or greater than.

What is it I do not understand here? :slight_smile: Very happy for clarification.

Thanks in advance for your patience and help.

In the first version, the condition is a < 21. If the number is less than 21, then we continue to the next iteration of the loop by skipping all other statements in the body of the loop. So, if the number is less than 21, we don’t reach the print statement and instead immediately go to the next iteration of the loop. The net effect being that only numbers Equal to or Greater than 21 will be printed.

In the second version, the condition is a <= 21. If the number is equal to or less than 21, we continue to the next iteration by skipping the print statement. The net effect being that only numbers Greater than 21 will be printed.

2 Likes

Sometimes the examples don’t do real justice, as is the case here. But, the reader/learner will get a quick grasp of what is at hand here. and be able to use that in the logic they design in future. It is a special case scenario we are looking at, for the most part. Not all, or many loops need this sort of constraint or exit, but it happens, and in those cases we want to use this escape window carefully, and with good reason.

Say we want to iterate over a list and only return True if there are no even numbers. We have no control over the numbers, and they might not be ordered. It’s the raw numbers we’re examining.

y = True
for x in [1,1,2,3,5,8,13,21,34,55]:
    if x & 1:
        continue
    else:
        y = False
print (y)

Of course we know in advance the outcome of this illustration, but the case is not always the same. Sometimes there are only odd numbers in a list.

With code this simple, we can jump into the shell without saving the code. It’s moot at this point…

>>> y = True
>>> for x in [1,1,2,3,5,8,13,21,34,55]:
...     if x & 1:
...         continue
...     else:
...         y = False
... 
...         
>>> y
    False
>>>

Consider,

>>> y = True
>>> for x in [i * 2 - 1 for i in range(1, 11)]:
...     if x & 1:
...         continue
...     else:
...         y = False
... 
...         
>>> y
True
>>> 
1 Like

@mtf @mtrtmk Thank you both for very good answers! It really cleared up much of my confusion.

2 Likes