Even after you learn the syntax to properly write for and while loops, you can easily make mistakes in your logic. Logic errors are the most difficult ones to find because first you have to notice the error (you will not get an error message, like a compile error), then you have to figure out where the problem is in your code, and then you have to figure out how to fix it.
One of the most common errors in writing loops is to have youyour loop go one too many or one too few times through. If the number of iterations for your loop is incorrect, do not just start changing code. First you need to understand what is happening, then fix it correctly. If you find that you are adding or subtracting 1 to a variable once a loop finishes, it probably means you are not executing the loop the correct number of times. This is the problem you should fix, don't adjust your variables; it's probably wrong and you will spend a lot of time trying to get it to work in every case.
One of the other most common errors in writing loops is to code a loop that never ends. This is called an infinite loop. If your program has an infinite loop, it may:
Coding an infinite loop is generally not a good idea, and it should never happen by accident. For now, any infinite loops are logic errors that must be fixed. If you notice an infinite loop as a result of testing your code, type CTRL-C (press the Ctrl key and the letter c at the same time) to get your program to stop.