CONTINUE in Python FOR Loops

🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!

🖥️ CONTINUE in Python FOR Loops

In each example you have seen so far, the entire body of the FOR Loop is executed, on each iteration. It is all very well, but quite limited, Right? Fortunately for you, Python provides two keywords that will terminate a loop iteration, prematurely: BREAK and CONTINUE in Python.

Great! Let’s dig a little deeper, and suppose this time you do not want to exit the loop with BREAK, but just skip the current iteration, and execute the rest of the iterations anyway – with the next items.

The Python CONTINUE statement on the other hand, will immediately terminate the current loop iteration. The execution jumps to the top of the loop and re-evaluate the expression, to determine whether the loop will execute again, with the next iteration, or if it will terminate and proceed to the first statement following the loop – like it would, with “break” reserved keyword you have seen before.

     for var in sequence:
         statement_1
         statement_2
         if condition:
             continue
         ...
         statement_3
         
     following_statement

Let’s have a look at the same example you have seen, in the break statement. But, instead of break, you will now use the continue statement. Continue is also a control statement, but the only difference is that it will only skip the current iteration in the loop, continues with the next iteration, then executes the rest of the iterations anyway, until it reaches the last item of the sequence.

     colours = ['red', 'green', 'blue', 'yellow']
     
     for colour in colours:
         if colour == 'blue':
             continue
         print(colour)

In the above example, as soon as the loop encounters the item ‘blue’, it will enter the if statement block, where the continue statement will skip the current item in the loop, then continues to the next iteration, with the item yellow. This, will result in printing out ‘red’, ‘green’ and ‘yellow’ – since blue has been skipped over.

Let’s play this video, stick around and watch until the end of this video! 👍🏻

– Digital Academy™ 🎓

***

☞ WATCH NEXT:
○ Data Types in Python – https://youtu.be/cweUByxBWiU
○ Operators in Python – https://youtu.be/-wDaVLkKOiU
○ IF Statements in Python – https://youtu.be/CC5seZ6OBJ4
○ FOR Loops in Python – https://youtu.be/JgH-D5DSTho

☞ WATCH MORE:
○ HOW TO Learn Python? Python Tutorial for Beginners [FULL Course] https://youtu.be/9hvnSZPMtuw

📖 Blog: http://digital.academy.free.fr/blog/

📖 Complete Python Development Course for Beginners [PLAYLIST]: http://digital.academy.free.fr/playlist/python-development-for-beginners

🛒 Shopping and Discounts: http://digital.academy.free.fr/store/

#Python #Tutorial #Beginners #Shorts

***

♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.

***

♡ FOLLOW US ♡
http://digital.academy.free.fr/
https://twitter.com/DigitalAcademyy
https://www.facebook.com/digitalacademyfr
https://www.instagram.com/digital_academy_fr/
https://www.youtube.com/c/DigitalAcademyOnline

♡ SUPPORT US ♡
http://digital.academy.free.fr/join
http://digital.academy.free.fr/donate
http://digital.academy.free.fr/subscribe
https://www.patreon.com/digital_academy
https://www.buymeacoffee.com/digital_academy

***

Copy link
Powered by Social Snap