Break Statements
The break is a keyword in python which is used to bring the program control out of the loop. The break statement breaks the loops one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops. Break is used to abort the current execution of the program and the control goes to the next line after the loop.
Below given are some of the break statements in Python:
count = 1
for item in my_list:
if item == 4:
print("Item matched")
count += 1
break
print("Found at location", count)
----------------------------------------------------------------------------------------------------------------------------
- my_str = "python"
for char in my_str:
if char == 'o':
break
print(char) - -----------------------------------------------------------------------------------------------------------------------
while 1:
print(i," ",end=""),
i=i+1;
if i == 10:
break;
print("came out of while loop");
----------------------------------------------------------------------------------------------------------------------
while True:
i = 1
while i <= 10:
print("%d X %d = %d\n" % (n, i, n * i))
i += 1
choice = int(input("Do you want to continue printing the table? Press 0 for no: "))
if choice == 0:
print("Exiting the program...")
break
n += 1
print("Program finished successfully.")
Comments
Post a Comment
Please comment