Jump Statements in Python

Jump statements let you jump from one part to another in a program. Usually, they are used inside loops to exit the loop with finishing all the iterations.

These statements get activated on a certain condition and skip a piece of code. The code can be skipped using jump statements in three different ways. For this, Python provides the following statements:

  1. Break Statement
  2. Continue Statement
  3. Pass Statement

1. Break Statement

As the name suggests, the break statement breaks the loop and the control jumps to the instruction next to the loop body.

Break statement is always placed under an if statement, so that when the condition becomes true, the break statement gets activated and terminates the loop. This causes the control to exit the loop body.

Break statement simply contains a break keyword.

Syntax:
break

Flowchart:

2. Continue Statement

Continue statement is used when you want to skip a particular iteration and continue the loop again. It is created using continue keyword.

In case of continue statement, the execution of the current iteration stops and the control jumps back to the beginning of the loop. Continue statement is also placed under an if statement.

Syntax:
continue

Flowchart:

3. Pass Statement

Pass statement is a blank statement that does nothing. It is used when you do not want to use a piece of code, but you cannot omit it from your program.

Pass statement is created using pass keyword which performs no operation and sends the control to the next statement.

This may seem like a comment, but unlike comment, pass statement is not ignored by the interpreter.

Syntax:
pass

Flowchart:

Example

Let us create a program using for loop to print the values from 1 to 10 with the help of range() function.

for i in range(1,11):
     print ("This is iteration no. ", i)

Output:

This is iteration no. 1
This is iteration no. 2
This is iteration no. 3
This is iteration no. 4
This is iteration no. 5
This is iteration no. 6
This is iteration no. 7
This is iteration no. 8
This is iteration no. 9
This is iteration no. 10

Now, let us implement break, continue and pass statement in the above program.

for i in range(1,11):
if i==1:
   pass
if i==5:
   continue
if i==8:
   break
print ("This is iteration no. ", i)

Output:

This is iteration no. 1
This is iteration no. 2
This is iteration no. 3
This is iteration no. 4
This is iteration no. 6
This is iteration no. 7

Explanation:

Look carefully at the output of the second program. You will notice two changes.

  • First, iteration no 5 is missing. This is the result of the continue statement. As the condition (if i==5) is satisfied, the control goes back to the for loop instead of the print statement and "This is iteration no 5" is not printed.
  • Second, no statement is printed after the 7th iteration. This is the result of the break statement. As the condition (if i==8) is satisfied, the break statement is executed and the control jumps out of the loop body.
  • There is one more if condition used in the program ie (if i==1). This condition gets satisfied in the very first iteration itself and activates the pass statement. But the execution of the pass statement does not make any change in the output.