Why do I need continue in python?

Hi all! First post!
I don’t fully understand when/why to use the CONTINUE feature in Python. I find that I can achieve the same output with and without using CONTINUE.

For example:

food = [“apples”, “pears”, “bananas”, “steak”]
for i in food:
… if i == “apples”:
… continue
… print(i)

pears
bananas
steak

food = [“apples”, “pears”, “bananas”, “steak”]
for i in food:
… if i != “apples”:
… print(i)

pears
bananas
steak

Thank you in advance! :smile:

Hi, welcome to the forums.

This might be of some help:
https://pythonexamples.org/python-continue/

Hi @lisalisaj, thank you for sharing, but it really doesn’t answer my question. I don’t see how continue is valuable? Can you or someone show me an example where it makes more sense to use CONTINUE rather than the way I did it above? In fact, my method above uses one less code line. So it seems like it may be better…

Is there no explanation of loop control statements in the lesson? (do you have a link to it?)

continue is an example of a loop control statement. you use it when you want to continue with the logic in the loop/the execution of the next iteration of the loop. (break and pass are other examples of control statements that change the execution of the loop logic).

https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops

https://www.geeksforgeeks.org/python-continue-statement/