Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
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 break jumps out of loop entirely.
Let me save this and run it. That terminates the loop. It just brings the first 3 characters, and when it gets that first s, it stops entirely. So, that's break and continue. Finally, there is one more control in Python. It's called else, and it works just like else does in an if statement. If I say else here, and print ('else') and get rid of this break, what this does is this gets run whenever this condition becomes completely false.
In other words, when the iterator is out of stuff to iterate, then else gets used.
So, if I save this and run it, you see that it prints the entire string and
then it prints else.
And else also works in a while loop - actually all of these controls also work in while loop.
I am just going to show you this one. I am going to do this as a while loop
here, we'll say i= 0 and while (i
All right, here we go. So, let's save this and run it, and we see that does exactly the same thing. So, when this condition becomes false, in other words when we've incremented past the end of the string, then else gets used, and it prints the else down there. So, that's break and continue and else, and these are controls that will break entirely out of loop. will shortcut a loop with continue, or else is used when the condition becomes completely false, or if it's never true in the first place.
So, that's break, continue and else.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98592 Viewers
61 Video lessons · 85891 Viewers
71 Video lessons · 69770 Viewers
56 Video lessons · 102077 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.