break
` statement in a `for
` loop :# Using the break statement in a for loop
numbers = [1, 2, 3, 4, 5]
for number in numbers:
print(number)
if number == 3:
break
print("Done.")
1
2
3
Done.
for
` loop to iterate over a list of numbers. Inside the loop, we use the `print()
` function to print each number in the list.if
` statement to check if the current number is equal to `3
`. If it is, we use the `break
` statement to exit the loop immediately.print()
` statement outside the loop, which prints the message "Done
."break
` statement to exit a loop prematurely based on a condition. In this example, we exit the loop as soon as we encounter the number `3
`.