How can we exit a loop?



In the context of this code challenge, how can we exit a loop?


In Python, the main way to exit a loop is using the break statement. When the break statement runs in a loop, it will terminate that loop.

However, one thing to keep in mind is that break statements will only terminate the innermost loop that it is run inside. So if you have a nested loop, the outer loop will continue to run.

You can also exit a loop using the return statement, but only if the loop is inside of a function. Furthermore, the return statement will not only exit the loop, but will also exit the entire function, so be careful when using it.