Control loop flow with break, continue, and else Python 3


show more Controlling loop flow with break, continue, and else provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training show less
please wait ...

Controlling loop flow with break, continue, and else

Sometimes when you are writing a loop you need a way to shortcut the loop, or to break out of the loop, and Python has controls for this. Let's go ahead and make a working copy of loopcontrol.py. I'll call this loopcontrol-working.py and go ahead and open that working copy. Here, we have a simple loop that prints out each character of a string. It looks like it's just printing the string, but it's actually doing it one character at a time in the loop. I'll go ahead and run this, and you'll see that's what it looks like.

Let's say that we wanted to skip all of the letter Ss. We could simply say if c =='s': and continue. What continue does is it shortcuts the loop. It just goes back to the beginning and gets to the next iteration without doing whatever comes after it. If we save this and run, we see that we skip all the Ss. On the other hand, maybe we want to end at the first s. Maybe we were just looking for the first s. So, we can use break, and bre...

Controlling loop flow with break, continue, and else
Video duration: 2m 43s 6h 36m Beginner

Viewers:

Controlling loop flow with break, continue, and else provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training

Subjects:
Developer Web
Software:
Python
Author:
please wait ...