JUMP Statements in Python

JUMP STATEMENTS

Looping allows a user to program and repeat tasks efficiently. In certain situations, when some particular condition occurs, a user may want to exit from a loop (come out of the loop forever) or skip some statements of the loop before continuing further in the loop. These requirements can be taken care of by use of break and continue control statements respectively. Python provides these jump statements as a tool to give more flexibility to the programmer to control logic of loops.

Break Statement

The break statement terminates the current loop, i.e., the loop in which it appears and resumes execution at the next statement immediately after the end of that loop. If break statement is inside a nested loop (loop inside another loop), break will terminate the innermost loop.

Syntax:

continue Statement

When a continue statement is encountered, the control jumps to the beginning of the loop for next iteration, thus skipping the execution of statements inside the body of loop for the current iteration. As usual, the loop condition is checked to see if the loop should continue further or terminate. If the loop’s condition is still true, the loop is entered again else the control is transferred to the statement immediately following the loop.