[Tutor] How and where to use pass and continue

Kevin python.programming at gmail.com
Mon Mar 28 05:13:45 CEST 2005


That was a great help I understand now what they do and how to use
them. Thanks alot for all your help.


On Sun, 27 Mar 2005 21:43:45 -0500, Bill Mill <bill.mill at gmail.com> wrote:
> On Sun, 27 Mar 2005 20:37:02 -0500, Kevin <python.programming at gmail.com> wrote:
> > I am having lot of trouble learning where and when to use pass and
> > continue. The two books that I use don't explian these very good. Is
> > there a website the explains these is great detail?
> > I have also looked at the python tutorial as well.
> 
> Kevin,
> 
> I'll try to help you out - pass and continue are pretty simple
> concepts. Consider the following code snippet which I will try to use
> to explain both:
> 
> command = None
> while command != '3':
>     command = raw_input("Press 1 to pass, 2 to continue, or 3 to exit ")
>     if command == '1':
>         print "passing"
>         pass
>     elif command == '2':
>         print "continuing"
>         continue
>     else:
>         print "othering"
>     print "end of loop reached"
> print "exiting"
> 
> PASS
> 
> The 'pass' statement simply means 'do nothing'. In the example above,
> when the python interpreter encounters the pass statement, it simply
> continues with its execution as it normally would.  It is usually used
> as the only statement in the body of an if statement to denote
> explicitly that nothing is to be done. I will often use it as a
> placeholder so that a program compiles correctly, like:
> 
> if 'a':
>     do_something()
> elif 'b':
>     #TODO: implement do_something_else()
>     pass
> elif 'c':
>     quit_foo()
> 
> Without the pass statement, there are no statements in the second
> block, and python will raise a SyntaxError.
> 
> In the first example above, Python sees the pass, exits the series of
> 'If...elif..." conditions, advances to the final statement of the
> while loop, prints "end of loop reached", and resumes execution at the
> top of the loop.
> 
> CONTINUE
> 
> The continue statement means what it says - continue with the loop,
> but resume execution at the top of the loop. In the case of a while
> loop, the exit condition will be evaluated again, and execution
> resumes from the top. In the case of a for loop, the item being
> iterated over will move to its next element. Thus,
> 
> for i in (1,2):
>     print i
>     continue
>     print "we never get here"
> 
> Will print 1, hit the continue, update i to the value 2, print 2, hit
> the continue, and exit because there are no more iterations for i.
> 
> In the first example I gave, after python reaches the continue,
> 'command' is again evaluated to see if its value is 3, then the loop
> proceeds from the top down. If you run the example, you should be able
> to figure out what's going on.
> 
> There are a couple more wrinkles - for example, continue only works on
> the innermost loop in its execution context - but generally, they work
> as you expect. The longer you work with python, the more you'll find
> this to be the case, but I'm biased.
> 
> Hope this helps, and feel free to ask questions about what you don't understand.
> 
> Peace
> Bill Mill
> bill.mill at gmail.com
> 
> >
> > Thanks
> >
> > Kevin
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>


More information about the Tutor mailing list