Loading video player…

Getting to Know the break Statement

00:00 Loops are a key control flow structure found in most languages, Python included. What’s control flow? Well, when programming was new, you had programs where code could only run in order line by line from the top of a program to the bottom.

00:13 But it didn’t take long writing code like that to realize its severe limitations. And this is where the concept of control flow structures comes in. They direct code execution, allowing complex programs with many branching paths.

00:25 There’s lots of different kinds, but in this course, we focus on loops, the control flow structures that enable repeating code blocks a fixed number of times or based on some condition.

00:34 There’s two different kinds of loops you’ll find in Python: for loops, which iterate over a collection of items ending when that collection is exhausted.

00:42 They also have a fixed number of iterations, the only exception being if the collection happens to have infinite items, but we won’t worry about that today.

00:50 for loops are commonly used when you need to take each element in a collection and do something with it, process it, validate it, etc. Then you have while loops. They repeat a code block based on some condition, continuing to run while that condition is true.

01:04 As a result, they have potentially infinite iterations. while loops are great for when you don’t know in advance how many times you need to loop, often used for actions like gathering user inputs or managing the event loop in games or other interactive apps.

01:16 Usually you won’t actually want your while loops to run forever, and often, you don’t want your for loops to go through all of the items in their collections.

01:24 This is where the break keyword comes in.

01:27 The break keyword can be used with either kind of loop, and exits the loop immediately with code execution resuming after the loop. Similar to break in Python, you’ll also find the continue keyword. continue doesn’t exit a loop, but instead skips the remainder of the loop body, meaning that code execution resumes with the next iteration of the loop. With while loops, this means they start by checking the condition in the loop pattern.

01:50 Now open the REPL and we’ll take a look at an example of each of these in action.

01:56 First, create a list to iterate over. Call it fruits.

02:01 fruits = equals the list of strings ["apple", "guava", "melon", "banana", "lime"].

02:08 So a straightforward loop that prints each element in the list would look like this: for fruit in fruits: print(fruit). The variable fruit takes on the value of each element in the list one at a time and is then printed.

02:21 So the output is all five fruits. If you want to find “banana” in the list and stop processing the list at that point, you can use break: for fruit in fruits: if fruit == "banana": break otherwise, print(fruit). Run the loop and you see apple, guava, melon.

02:43 No banana and no lime because the loop stopped at banana. If instead you wanted to just avoid printing “banana”, you could use continue: for fruit in fruits:

02:54 if fruit == "banana": continue otherwise, print(fruit).

03:03 And you see apple, guava, melon, and lime because when banana is found, continue moves on to the next item, skipping printing banana. And what if you use break outside of a loop?

03:14 Yeah, you get a syntax error: break outside loop. No further explanation needed. Alright, that’s basic usage. I hope you enjoyed these fruit loops.

03:25 Next up, we’ll focus on a more practical example of using break with a for loop. See you there.

Become a Member to join the conversation.