If you happen to have an infinite loop in your code and want to fix it, there are some steps can you can do to try and debug or fix it so that the Do or While loop will end. Let's say you want to run the loop 10 times but when you run the program, it continues to run the loop continuously instead of stopping after 10 times.
Before you move on, it's always good to do some real-time debugging for Do and While loops. Place a command that will show you the value of the index. It depends on what programming language you are using but for the sake of our example, you can use print, writeln, msgbox, etc. to display the value of the index. This way, you'll know how many times the loop has run or what values the index acquires after each loop. If it goes way over the number of intended times it's supposed to run, you may possibly have an infinite loop. The index is the variable that we usually assign to count the number of the times the loop has run.
Let's use a While Wend loop as an example (for the BASIC programming language).
X=0
While X = 0
Print "Hello"
Wend
In this case, the loop will run while the value of X = 0. Let's call X as the index of the loop. If the value of X changes, the loop will terminate. In this While loop, it will print the word Hello infinitely because the value of X is 0 and it does not change. To verify this, add Print X below the Print "Hello" statement. The program will display the value of X (which is 0) after it displays Hello. You'll know immediately that the loop will run forever since the value of X will always be 0. But if you place X = X + 1 before Wend, after the first execution of the loop, the loop will end because the value of X is now 1. So when your conditions are met within the loop you're trying to make, make sure you change the value of the index.
For Do Until loops, it's the other way around. It will continue to run until the index is equal to the value in the Until statement. For the For Next loop, you don't have to worry too much since you usually have a start and end point. If you need to loop a certain number of times, make sure that your index increments until it meets the condition of the loop. Let's say you want the loop to end after running 10 times. Make sure to increment the index by 1. Or if it is not a numeric value, make sure that you met the condition within the loop for the While/Do loop to terminate.
These are just a few of the different scenarios that can happen in infinite While and Do loops. The bottom line is that when dealing with infinite loops (regardless of what type of loop it is), real-time debugging helps a lot and make sure that your index is always met within the loop.
Published by Aaron Tadeo
Writing has become one of my hobbies and I really love the feeling when I share my experiences and knowledge as a freelance writer. I'm currently working as a customer service rep. I love computers and been... View profile
- How To: Remove Programs Compiled from Source Code in LinuxThree step reference to removing software manually compiled from Source Code, using the Linux terminal.
RAPTOR Programming Language OverviewAn overview of the RAPTOR programming language that is used by the United States Air Force Academy and other colleges as an introduction into programming.- The Best Approach to Learning Computer ProgrammingIf there is a growing industry world wide then it is the industry of computer programming. Nowadays some of the computer programming languages are available free of charge online and anyone can become a computer progr...
- Looping Statements in Java
- The Best Computer Programming Language for Beginners
- Computer Programming Language Comparison
- Computer Programming - Then and Now
- Facts About Open Source Software Development
- The Effect of Feedback Loops on Climate Change
- GrimGrimoire for the PS2: Video Game Review




