Snippets - Python
The code creates an array containing three numeric values. The for loop iterates through the array, checking each number for validity. If a number is negative, the code prints "Negative number detected!" and breaks from the loop. If the number is valid, the code prints the number.
In a loop, the break keyword escapes the loop, regardless of the iteration number. Once break executes, the program will continue to execute after the loop.
numbers = [0, 12, 42, -51, 83]
for num in numbers:
if (num < 0):
print("Negative number detected!")
break
print(num)