The break and continue Keyword
In this lesson, we'll learn the use of 'break' and 'continue' keyword in loops.
We'll cover the following
When adding a loop, we always decide the terminating condition. Let’s say we need to print first n
numbers; so we can write our loop as:
for n in range(0, 10):
print(n)
The break
Keyword
What if, we need to exit a loop before it reaches the end?
Suppose we have first ten natural numbers in a box, and we have to find whether x
number exists in that box or not. If x
is less than 10, we don’t have to run loop times. We can stop, once we find x
.
That’s what the break
keyword is for. It can break the loop whenever we want.
Get hands-on with 1200+ tech skills courses.