Image Source: www.pexels.com
The infinite loop also called endless loop is a type of loop that never ends. Instructions inside the loop will execute endlessly.
The reason behind the occurrence of Infinite loop
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.
The harm that Infinite Loop can do
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.
How to Stop Infinite Loop
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.
Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don?t feel hesitate that you can?t do that or you don?t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.
Wanna get in touch with me? Here are links. I?ll love to become your friend. ?
or just mail me at firstname.lastname@example.org