Difference Between Break and Continue in Python
In this article, you will learn about the Loop control statement , the necessity of using these statements to control the loop's flow, and their use according to our requirements. Parallelly, to understand sys.a rgv command line argument , click here.
The article has been organized in the following manner:
- Introduction to Loop Control Statement
- Break statement
- Syntax of break statement
- Flow diagram and steps involved
- Example of break statement
- Continue Statement
- Syntax of continue statement
- Flow diagram and steps involved
- Example of continue statement
- Pass statement
- Syntax of pass statement
- Example of Pass statement
- Difference between break and continue statement
- Conclusion
Introduction to Loop Control Statement
Break and Continue statements are also known as Loop Control Statement s , as they automate the working of the inner loop. L oop automates the task and repeats in a certain manner, but there are some special case s where we need to terminate the loop or skip some iteration. There we use a Loop Control Statement that is Break and Continue.
Python Supports 3 Basic Loop Control Statements
- Break Statement
- Continue Statement
- Pass Statement
Break Statement:
It works similar to a break statement in C. It terminates the current working loop and passes the control to the next statement, and if the break statement resides inside the nested loop, it passes control to the outer loop. It can be used with both while and for loops.
Forms of Break Statement:
- Label l ed break statement
- Unlabel l ed break statement
Label l ed break statement:It terminates the loop's outer statement, which has been marked with the given label.
Unlabelled break statement:It terminates the innermost statement of the loop
Uses of break statement:
- It is used when we want to skip or terminate a certain statement.
- When some external condition is triggered, and it requires t he termination of the loop.
Syntax: break
Flow diagram of break statement:
Here are the step s involve d in the flow chart given above explaining a break st atement :
- Execution of loop statement begins .
- If the looping statement is true, iteration starts.
- If the looping statement contains a break statement, the iteration will terminate, and then the current loop will exit.
- When the condition completes its execution, it will return its output.
- If the looping statement is false, it will exit from the loop.
- End of the loop.
Example:
Let's consider the condition where we want to iterate a loop from 1 to 10 . When 6 is encounter ed , we want to break the loop and return to the outer condition. Here we use break statement with for loop and if statement.
#Demonstration of Python break statement #using for loop #loop from 0 to 9 for i in range(10): # if i is equal to 6 # break statement terminate the current working loop if i == 6: break else: print (i)
Output:
0 1 2 3 4 5
Continue Statement:
It is the second form of the Loop Control Statement, very similar to the break statement. But when it comes to the working of the continue statement, it works just opposite to the break statement . I nstead of terminating certain conditions, it jumps off to the very next condition. But it will continue the execution of the loop statement as per its name.
Forms of continue statement:
- Label l ed continue statement
- Unlabelled continue statement
Label l ed continue statement: It skips the iteration of the outermost loop, marked with the label.
Unlabelled continue statement: It s kips the iteration of the innermost loop.
Uses of Continue statement:
- When we don't want certain conditions to run.
- When we don't want to terminate the loop and just want to skip iteration.
- When we want to continue the iteration.
Syntax : continue
Flow diagram of continue statement:
Here are the steps involved in the flow chart given above explaining a continue statement:
- Execution of loop statement begins .
- If the looping statement is true, iteration starts.
- If the looping statement contains a continue statement, the iteration will skip the condition statement and then jump to the next iteration statement.
- When the condition completes its execution, it will return its output.
- If the looping statement is false, it will exit from the loop.
- End of the loop.
Example:
The continue statement is used in the program to skip any iteration, such as when we don't want to print a certain statement. Consider the example of a break statement altering the condition. There are numbers from 1 to 10, but we don't want 2 to come a nd we don't want to increase the number of looping statements in our program either. For this we should use a continue statement.
Refer to the below implementation:
#Demonstration of Python continue statement #using for loop #loop from 0 to 9 for i in range(10): # if i is equal to 2 # continue statement will skip #and start executing next iteration if i == 2: continue else: print (i)
Output:
0 1 3 4 5 6 7 8 9
Pass statement:
Pass statement is not found in traditional C language; it works like a null operation because nothing happens when the pass statement is executed. It is used in P ython when any statement is required syntactically, but we don't want any command or code lines to be executed.
- Pass statement is also known as the placeholder, which can be used inside the function call, classes, and lopping or control statement.
- It can be used as a declarative statement, which can be implement ed later on when required.
- It is just a block of code that runs when called and works the same as the null statement.
- The best place to use the pass statement is Stubs.
Uses of Pass Statement:
- It can be used in Stubs.
- It can be used while writing empty loops.
- It can be used for empty control statements.
- It can also be used to implement empty functions and classes.
Syntax: pass
Example:
#Dem o nst r ation of Python pass statement #using for loop #loop from 0 to 9 for i in range(10): # if i is equal to 6 # pass statement will execute #but there will be no impact #on the looping statement if i == 6: pass print 'Just a pass statement' else: print ( i )
Output:
0 1 2 3 4 5 Just a pass statement 7 8 9
Difference between break and continue statement:
- The main difference between both the statement s is that when break keyword comes , it terminates the execution of the current loop and passes the control over the next loop or main body, whereas when continue keyword is encountered , it skip s the current iteration and execute s the very next iteration in the loop.
- In simple words break statement leaves a loop, whereas the continue statement jumps to the next iteration.
Conclusion:
These are the loop control statement s , which help us to terminate or skip some required statement under the normal procedure . Every statement has its function, and none of them can be considered to be better than the other, as they each have different aspects and usage.
Source: https://www.knowledgehut.com/blog/programming/break-and-continue-statement
0 Response to "Difference Between Break and Continue in Python"
Postar um comentário