CONTINUE KEYWORD IN PYTHON | Python Continue

Python Continue

Now, we are going to learn about a very interesting and useful keyword, which is the continue keyword. Well, as the name of the keyword says, when the continue keyword executes, it just skips the current iteration, and moves to the next iteration. When we would have a look at a simple example, you would understand the significance of the continue keyword. So, first of all, Let’s have a look at a very simple program, which demonstrates the use of the continue keyword.

Python Continue

As you can see in the above program, we are running a for loop from 1 to 20(since 21 is excluded), and in the loop, we are checking if the number is divisible by 2, and if the condition is True, then we are skipping that iteration. So, when the continue keyword executes, it is like just skip everything for this iteration, and move to the next iteration. So, if the even number is encountered, then it won’t be printed as our output. So, we are going to get only odd numbers as our outputs. So, the output of the above program comes out to be something like this –

1 3 5 7 9 11 13 15 17 19

As you can see, we have all the odd numbers as our output. So, using the continue keyword, we have skipped that particular iteration, and just moved to the next iteration. So, as and when required, we can simply make use of the continue keyword. You can also try some other programs, related to the continue keyword, which would make you familiar with the concept. Also, you can try using the continue keyword with the while loop. Remember that when the continue keyword executes, it is like just skip everything for this iteration, and go to next iteration.