Jump Statements in Python-continue statement

Pre-requisite: Loops in python, Jump statements in python-break

Continue statement is the second jump statement that provides us loop control. In this article, we will be learning about the functionality and importance of a continue statement.

We discussed about the break statement earlier. It terminates the whole loop if a particular condition becomes true. We talked about how we need the loop to skip some iterations and execute for other iterations, and for this to happen, we use the continue statement.

Let us take the example from the break statement in the previous article:

Imagine we write a program where the user can input values a defined number of times; we keep adding the inputs. If the user gives a negative number, instead of terminating the whole loop, we should not add the number rather ignore it and ask for the next number.

Here, using the break statement is ruled out. We need something using which we can skip the negative number iteration, which is the continue statement's functionality.

Definition:

A continue statement is a jump statement that provides us the loop control to completely skip or ignore an iteration of the loop if a condition satisfies. It skips the whole iteration and continues to the next iteration as its name suggests.

  • As soon as the interpreter finds a continue statement in the code, it terminates the iteration and transfers the control to the loop again to continue with the next iteration.

Syntax:

Flowchart:

Jump Statements in Python-continue statement

As you can observe, if the continue statement is executed; the control is shifted back to the loop skipping that particular iteration only.

Working of the continue statement inside for and while loops:

Jump Statements in Python-continue statement

Important points:

  1. Like a break statement, a continue statement can be used in all the loops.
  2. Unlike break, it cannot be used in the switch-case statement.
  3. "continue" is a reserved keyword in the python libraries.
  4. It is also almost always written in an if statement inside the loop body.
  5. We cannot use a continue statement without a loop. It throws an error:
    "SyntaxError:'continue' outside loop".
  6. If we use a continue statement in the inner loop of a nested loop, then the inner loop iteration will be skipped. The outer loop won't be affected.
  7. We cannot use labels, conditions, or other options with the continue statement.

Example situation:

Consider the above example. We need to skip if the user enters a negative number. We should now design a program to calculate the sum of only positive numbers in the 10 random inputs the user gives.

Code:

Output:

Enter a number: 1
Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: -9
Enter a number: 6
Enter a number: 7
Enter a number: -3
Enter a number: 9
Enter a number: 10
The sum of the 10 positive numbers you gave:  42

Understanding:

As you can observe, in the 4th and 7th iterations, the user gave negative inputs- -9 and -3. Inside the loop, we check if the input given by the user is greater than 0. If not, the "continue" statement is executed and that particular iteration is ignored (skipped), and the next iteration of i+1 is executed.

Example programs:

Output:

The wrong spelling mentioned earlier: ['D', 'I', 'R', 'E', 'C', 'U', 'T', 'I', 'O', 'N']
The filtered correct spelling: ['D', 'I', 'R', 'E', 'C', 'T', 'I', 'O', 'N']

Understanding:

Inside the loop, we checked if all the characters in the list spelling are of the correct spelling "DIRECTION". If yes, we appended those characters into the list "correct". If not, we used the continue statement. As the character "U" is not in the word's correct spelling, the letter got skipped. You can observe the difference between the two lists in the output.

Output:

The natural numbers in the list: [3, 4, 2]

Understanding:

We wrote this program to print the natural numbers in the list if there are any. There are a 0, two negative numbers, and a decimal number, as you can observe in the list. We iterated the values in the numbers, and inside the loop, we checked if the number was positive and an integer number. If yes, we append the value into a new list nat_num. If not, we skip the iteration using the continue statement in the else block.

Output:

* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 
* * * * * * * * 
* * * * * * * * * 
	* * * * * * * * * *

Understanding:

Here, we used the continue statement to print the *s' until we print the right number of *s' in a row, then skip the next iteration. If the current star count is less than the line number, we print the stars and then skip by incrementing the value of the star; else, we go to a new line (new row).

In a nested for loop:

If the if condition becomes true, then the continue statement is executed, the iteration of j in the inner loop is skipped, and the next iteration is continued for j+1. It does not affect the outer loop.

Labeled continue statement:

Just as the name suggests, a labeled continue statement will have a label attached on which loop to iterate. Here is an example:

We can skip the outer loop iteration using the labeled continue instead of only the inner loop. But, this is not legal and defined in our python language. Other popular programming languages use this mechanism, but it is not allowed in python.

A PEP is raised to the python community to add this labeled continue statement in the language. Still, it is denied stating it adds unnecessary complexity to the code and the language.






Latest Courses