Mark As Completed Discussion

Important Tips when working with Loops

There are certain important keywords that one must be aware of while working with loops.

  • break statement is an important keyword when working with loops. It can force stop a loop during the execution, before the terminating condition is set to false (in a while loop), or before it completes all its number of iterations (in a for loop).
  • continue statement allows to stop the current iteration and continue with the next. All the code below the continue statement will not be executed and the loop skips to the next iteration.
  • Infinite loop is a condition that occurs if the loop keeps executing forever. This is a common error that may arise when you begin to work with loops. The common cause of the error is usually an error in the loop condition or initialization. The only way to stop an infinite loop is to force stop the execution. This is why it's important to check your loop conditions and initializations, and be careful with them else your program will never stop executing!