Difference between break and continue in Python







  • What is the difference between break and continue in Python

    In python, break forces the execution to exit or “break” a for or while conditional loop. The continue statement is used to skip code within a loop for certain iterations of the loop.
    In Python, break and continue statements can alter the flow of a normal loop. Loops iterate over a block of code until test expression is false, but sometimes we wish to terminate the current iteration or even the whole loop without cheking test expression. The break and continue statements are used in these cases. break - Jumps out of the closest enclosing loop (past the entire loop statement) continue - Jumps to the top of the closest enclosing loop (to the loop’s header line) break statement: The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop. If it is inside a nested loop (loop inside another loop), break will terminate the innermost loop. continue statement: The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration.
  • python bootcamp 2021
datai analytics