Using Pass Versus Continue in Python

A number of people have asked me about the discussion of the pass and continue clauses on page 140 of Beginning Programming with Python For Dummies. The example on that page is confusing a lot of people. Most people assume that when the example prints its output, the w should not appear as part of the output—as if the pass and continue clauses work precisely the same.

If you look at the second sentence of the first paragraph on page 140, you see that it tells you that pass and continue work almost the same way, except that the pass clause allows completion of the code in the if block in which it appears. This distinction is important. The continue clause is immediate, the pass clause isn’t. So, yes, you can achieve different results using pass or continue.

However, both clauses work in the same way in that they stop execution of the current loop and continue with the next loop. The difference is when they stop execution of the current loop and it all hinges on the if statement block.

Enough people have written about this particular example that I want to be sure there is no confusion about the difference between pass and continue. Please let me know if you have any additional questions about these two clauses at John@JohnMuellerBooks.com.

 

 

Author: John

John Mueller is a freelance author and technical editor. He has writing in his blood, having produced 99 books and over 600 articles to date. The topics range from networking to artificial intelligence and from database management to heads-down programming. Some of his current books include a Web security book, discussions of how to manage big data using data science, a Windows command -line reference, and a book that shows how to build your own custom PC. His technical editing skills have helped over more than 67 authors refine the content of their manuscripts. John has provided technical editing services to both Data Based Advisor and Coast Compute magazines. He has also contributed articles to magazines such as Software Quality Connection, DevSource, InformIT, SQL Server Professional, Visual C++ Developer, Hard Core Visual Basic, asp.netPRO, Software Test and Performance, and Visual Basic Developer. Be sure to read John’s blog at http://blog.johnmuellerbooks.com/. When John isn’t working at the computer, you can find him outside in the garden, cutting wood, or generally enjoying nature. John also likes making wine and knitting. When not occupied with anything else, he makes glycerin soap and candles, which comes in handy for gift baskets. You can reach John on the Internet at John@JohnMuellerBooks.com. John is also setting up a website at http://www.johnmuellerbooks.com/. Feel free to take a look and make suggestions on how he can improve it.