0

For what purpose and why BREAK statement is used in loops in Phyton?

1/4/2020 7:51:29 AM

Lalit kumar

4 Answers

New Answer

+2

for example, in order to exit the cycle ahead of time

+2

When you don't want to repeat loop anymore. for example in linear search to check whether element exists or not or to check it's index. you compare key element with elements of an array/list and when element is found exit loop using break . read this code: fruits=["mango","pinapple","orange","apple"] reqfruit="orange"; #required fruit idx=-1 for i in range(0,len(fruits)): if(fruits[i]==reqfruit): idx=i break if(idx==-1): print(reqfruit," not found") else: print(reqfruit,"found at index ",idx)

0

you have ten numbers entered out of order. you need to find the number 5. in the loop, you iterate over each digit and insert another condition (if): if the desired number is 5, then the loop is interrupted (break)

0

Example: print('Enter as many words as you like!') words = [] while True: word = input() if not word: break words.append(word) print(words)