The infinite loop also called endless loop is a type of loop that never ends. Instructions inside the loop will execute endlessly.
Generally, there are six reasons that become the reason behind the occurrence of an infinite loop.
- No terminating condition
- Having one condition that can never be met
- Having a condition that causes the loop to start again and again
- Miss the incrementer or decrementer (if your logic needs one)
- continue statement in the wrong place (if your logic needs one)
- wrong comparisons in loop condition (less than or greater mixed up)
So whenever you get an infinite loop just check from these six conditions, there are high chances that you’ll get your mistake.
Mainly Infinite loop can harm your system in two ways:
By using your processor time and power — As a general rule if your processor is working on any process it takes time and power to do it. Same as any other process Once started infinite loop will use your processor time and power. And at one point in time, it slows down your system and made it unresponsive.
By using your memory — In computer mainly all running processes reside inside the RAM. And when an infinite loop occurs, it just started to fill up your RAM space and at one point of time your system will run out of memory and it can results in complete system failure.
Yes, that’s also true that modern operating systems like Windows 10, Linux and macOS have a pre-defined mechanism to handle this kind of processes. After reaching a certain level these operating systems can automatically kill these kinds of processes to avoid complete system failure. But again if implemented in a proper way an infinite loop can damage a lot.
If you’re a developer and if you have an infinite loop in the terminal application then (ctrl+c) will usually stop it. If it is graphical application then you can kill the process via Task Manager or if you are using IDE then find the stop button. In most cases, this will terminate your infinite loop.
So, guys, that’s it for an infinite loop. Don’t afraid if you have one. Every programmer meets with an infinite loop once in a life. Just drink a cup of coffee, keep your mind cool and check from above six conditions you’ll definitely find the solution.
Feel free to let me know if I miss something, I’ll love to learn it from you. Till then Keep Loving, Keep Coding. I’ll surely catch you up in the next article.
Jai Hind, Vande Mataram 🇮🇳
Wanna get in touch with me? Here are links. I’ll love to become your friend. 😊