7.2 C
New York
Sunday, December 4, 2022

Pass vs. Continue in Python Explained – Built In

Python’s flexibility makes it the best solution to automate and to do repetitive tasks in an efficient way.
In Python (and other programming languages), loops help to iterate over a list, tuple, string, dictionary and a set.
Loops iterate over a block of code until the test expression is false, but sometimes we wish to terminate the current iteration or even the whole loop without checking the test expression.
This can be achieved using a few keywords that can alter the flow or execution of the loops. In Python those keywords are — break, continue and pass. It’s important to know when and how to use these keywords to control the flow of your loop.
Therefore, I’ll discuss and provide examples for the when, why and how of break, continue and pass in Python. The concept behind these keywords is the same regardless of the programming language you choose to use.
Let’s get started.
 
The break statement in Python terminates the loop containing it.
For example:
The above for loop generates output as:
We’ve added an if statement to check a condition. When this condition becomes True, the program flow will go inside the if statement and execute the break statement.
Therefore, the for loop executed until the condition num == 5 becomes True. When the condition becomes True, the break statement is executed to terminate the flow of the for loop.
Now, let’s add one more for loop outside the existing for loop, as shown below:
The break statement will terminate the for loop containing it when the condition becomes True.
As we can see in the above picture, for every iteration of the outer for loop, the flow of the inner loop breaks after five iterations, as per the condition num == 5.
Thus, if the break statement is inside a nested loop (a loop inside another loop), the break statement will terminate the innermost loop. 
A real world example would be unlocking our smartphone with a password. Technically, the phone unlocking process is a for loop that continually asks for the password. When a correct password is entered, a break statement gets executed and the for loop gets terminated.
Below is the explanation with code:
In the above example, I set Python as the correct password. When the condition password == ‘Python’ becomes True, a break statement is executed and the for loop gets terminated.
More on Python: 13 Python Code Snippets You Need to Know
 
The continue statement is used to skip the remaining code inside a loop for the current iteration only.
For instance, let’s use continue instead of a break statement in the previous example.
When the condition num == 5 becomes True, the continue statement gets executed. The remaining code in the loop is skipped only for that iteration. That’s why Iteration: 5 is missing from the above output.
Therefore, the continue statement works opposite to the break statement. Instead of terminating the loop, it forces it to execute the next iteration of the loop.
 
As its name suggests, the pass statement does nothing.
It’s used when a statement or a condition is required to be present in the program, but we do not want any command or code to execute.
For instance, let’s replace the break statement in the previous example with a pass statement.
As we see in the above output, the pass statement really did nothing as the for loop and all statements within it are executed. 
The pass statement is typically used while creating a method that we don’t want to use right now. It’s often used as a placeholder for future code.
Most of the time, a pass statement is replaced with another meaningful command or code in a program.
More on Python: 3 Ways to Write Pythonic Conditional Statements 
 
There is a significant difference between pass and continue, and they are not interchangeable. 
continue forces the loop to start at the next iteration, whereas pass means, “there is no code to execute here,” and it will continue through the remainder of the loop body.
Here is an quick explanation of this difference:
In the above output, in each iteration of the for loop, the first two print statements are always executed even though they are after the pass statement. This means pass does nothing, and the flow of the loop goes un-interrupted.
However, the print statement after the continue statement isn’t executed, as the keyword continue forced the for loop to start the next iteration.
Summing up, a single example would be more helpful for understanding how pass continue and break work in Python.
As you can see in the above output, everything before and after pass is always executed, indicating the keyword pass does nothing.
Only the line before continue keyword is executed, indicating that continue always forces the for loop to start the next iteration and doesn’t execute anything after the keyword continue in the current iteration.
Finally, the line before break gets executed and the flow of the for loop gets terminated on the third iteration. This explains how the break statement is used to stop the process. 
Built In’s expert contributor network publishes thoughtful, solutions-oriented stories written by innovative tech professionals. It is the tech industry’s definitive destination for sharing compelling, first-person accounts of problem-solving on the road to innovation.

source

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles