In Python, break and continue statements can alter the flow of a normal loop.
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 test expression.
The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop.
If the break statement is inside a nested loop (loop inside another loop), the break will terminate the innermost loop.
The break statement in Python terminates the current loop and resumes execution at the next statement, just like the traditional break found in C.
The most common use for a break is when some external condition is triggered requiring a hasty exit from a loop. The break statement can be used in both while and for loops.
break
# Use of break statement inside loop
for val in "string":
if val == "i":
break
print(val)
print("The end")
Output
s
t
r
The end
The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration.
Example:
The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop.
The continue statement can be used in both while and for loops.
Example:
var = 10
while var > 0:
var = var -1
if var == 5:
continue
print 'Current variable value :', var
print "Good bye!"
This will produce following result:
Current variable value : 10Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Current variable value : 4
Current variable value : 3
Current variable value : 2
Current variable value : 1
Good bye!
continue
Pass Statement in Python
The pass statement is a null operation; nothing happens when it executes. The difference between a comment and pass statement in Python is that, while the interpreter ignores a comment entirely, the pass is not ignored. I t is used when a statement is required syntactically but you do not want any command or code to execute.
Example
The pass is also useful in places where your code will eventually go, but has not been written yet (e.g., in stubs for example) −
Syntax
passExample
for a letter in 'Python':
pass
print 'This is pass block'
print 'Current Letter :', letter
print "Goodbye!"
When the above code is executed, it produces the following result −
Current Letter : PCurrent Letter : y
Current Letter : t
This is pass block
Current Letter : h
Current Letter : o
Current Letter : n
Good bye!
Suppose we have a loop or a function that is not implemented yet, but we want to implement it in the future. They cannot have an empty body. The interpreter would complain. So, we use the pass statement to construct a body that does nothing.
# pass is just a placeholder for
# functionality to be added later.
sequence = {'p', 'a', 's', 's'}
for val in sequence:
pass
We can do the same thing in an empty function or class as well.
1.
2. def function(args):
3. pass
1.
2. class example:
3. pass
Post A Comment:
0 comments so far,add yours