While using the loops, we should keep in mind for the initialization, termination and update condition. Otherwise, there will be unexpected behavior of the program.
One of the most common mistakes while implementing any sort of looping is that that it may not ever exit, that is the loop runs for infinite time. This happens when the condition fails for some reason.
Java program to illustrate infinite loop:
Another pitfall is that you might be adding something into collection object through loop and program can run out of memory. Please try and execute the below program, after some time, out of memory error will be thrown.
Java program for out of memory exception: