xxxxxxxxxx
nums = [6,8,0,5,3]
product = 1
for num in nums:
if num == 0:
product = 0
break # stops the for loop
product *= num
print(product)
xxxxxxxxxx
#in Python, break statements can be used to break out of a loop
for x in range(5):
print(x * 2)
if x > 3:
break
xxxxxxxxxx
# Use of break statement inside the loop
for val in "string":
if val == "i":
break
print(val)
print("The end")
---------------------------------------------------------------------------
s
t
r
The end
xxxxxxxxxx
while True:
num = input("Enter the number: ")
if num == "":
break
number = int(num)
print("The square of the number ",number," is: ", number*number)
print("User entered a null string")
xxxxxxxxxx
for i in range(5): # For loop
if i == 3:
break # Exits out of the for loop, even when not finished
else:
print(i)
number = 1
while number <= 5:
if number == 4:
break # Exits while loop
else:
print(number)
xxxxxxxxxx
def function():
return # breaks out of a function
print('This does not happen')
xxxxxxxxxx
# Use of break statement inside the loop
for val in "string":
if val == "i":
break
print(val)
print("The end")