What is the difference between break and continue in Python?
In Python, both break and continue are keywords that control flow, but they have different purposes.
- The “break” keyword is used to terminate the current loop (for, while) and exit the loop body, executing the code after the loop. Once the “break” keyword is encountered, the loop will immediately stop.
- The keyword “continue” is used to skip the remaining code in the current loop and move directly to the beginning of the next iteration. When the continue keyword is encountered, the loop will immediately jump to the next iteration, avoiding the execution of any subsequent code in the current loop body.
In summary, ‘break’ is used to end the entire loop, while ‘continue’ is used to skip the current iteration of the loop.