For what purpose and why BREAK statement is used in loops in Phyton? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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

4th Jan 2020, 7:51 AM
Lalit kumar
Lalit kumar - avatar
4 Answers
+ 2
for example, in order to exit the cycle ahead of time
4th Jan 2020, 7:56 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 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)
4th Jan 2020, 8:06 AM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
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)
4th Jan 2020, 8:01 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
Example: print('Enter as many words as you like!') words = [] while True: word = input() if not word: break words.append(word) print(words)
4th Jan 2020, 10:36 AM
HonFu
HonFu - avatar