What is the use of infinite loop? Where does it use? What is "true" in given code? What is the use of it? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the use of infinite loop? Where does it use? What is "true" in given code? What is the use of it?

https://code.sololearn.com/cSmUTrcIMbHh/?ref=app

10th Dec 2017, 8:58 AM
SACHIN JOSEPH
3 Answers
+ 6
An infinite loop is a block of code that, intentionally or not, will repeat the execution of the loop body infinitely (never stop). By right, every loop we create must have a final condition, that is a where the requirements to evaluate a condition as true are met, and by then, the loop must cease repetition. Since you used while true: , your code may become an infinite loop, if it didn't have the condition checking branch, which you did using the following line: if text == "": break The lines above checks whether if the line that's read from the file has any content, and it instructed the loop to stop when a blank line was found (final condition is met) There is other condition set in the code, that checks if the line read from the file begins with # character, the branch than instructed that the loop to ignore the line, and continue to the next loop iteration: if text[0] == '#': continue Conclusion, without the if text == "": your loop is an infinite loop, it will keep on repeating the instructions, "forever". Hth, cmiiw
10th Dec 2017, 10:33 AM
Ipang
+ 4
You're welcome, glad to help : )
19th Dec 2017, 3:12 PM
Ipang
+ 3
thank you
19th Dec 2017, 3:07 PM
SACHIN JOSEPH