
Python Continue
You may confront a circumstance in which you need to leave a circle totally when an outside condition is set off or there may likewise be a circumstance when you need to skirt a piece of the circle and start next execution.
Python provides break and continue statements to deal with such circumstances and to have great control on your circle.
This instructional exercise will talk about the break, continue and pass statements accessible in Python.
The break Statement:
The break statement in Python ends the current circle and continues execution at the following articulation, much the same as the customary break found in C.
The most well-known use for break is the point at which some outside condition is set off requiring a rushed exit from a circle. The break statement can be utilized in both while and for loops.
Example:
#!/usr/bin/python for letter in 'Python': # First Example if letter == 'h': break print 'Current Letter :', letter var = 10 # Second Example while var > 0: print 'Current variable value :', var var = var -1 if var == 5: break print "Good bye!"
This will produce the following result:
Current Letter : P Current Letter : y Current Letter : t Current variable value : 10 Current variable value : 9 Current variable value : 8 Current variable value : 7 Current variable value : 6 Good bye!
The continue Statement:
The continue statement in Python restores the control to the start of the while circle. The continue statement dismisses all the leftover articulations in the current cycle of the circle and moves the control back to the highest point of the circle.
The continue statement can be utilized in both while and for loops.
Example:
#!/usr/bin/python for letter in 'Python': # First Example if letter == 'h': continue print 'Current Letter :', letter var = 10 # Second Example while var > 0: var = var -1 if var == 5: continue print 'Current variable value :', var print "Good bye!"
This will produce following result:
Current Letter : P Current Letter : y Current Letter : t Current Letter : o Current Letter : n Current variable value : 10 Current 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!
The else Statement Used with Loops
Python supports to have an else statement related with a circle articulations.
- In the event that the else statement is utilized with a for loop, the else statement is executed when the circle has depleted emphasizing the rundown.
- In the event that the else statement is utilized with a while loop, the else statement is executed when the condition turns out to be bogus.
Example:
The accompanying model delineates the mix of an else explanation with a for articulation that looks for prime numbers from 10 through 20.
#!/usr/bin/python for num in range(10,20): #to iterate between 10 to 20 for i in range(2,num): #to iterate on the factors of the number if num%i == 0: #to determine the first factor j=num/i #to calculate the second factor print '%d equals %d * %d' % (num,i,j) break #to move to the next number, the #first FOR else: # else part of the loop print num, 'is a prime number'
This will produce following result:
10 equals 2 * 5 11 is a prime number 12 equals 2 * 6 13 is a prime number 14 equals 2 * 7 15 equals 3 * 5 16 equals 2 * 8 17 is a prime number 18 equals 2 * 9 19 is a prime number
Similar way you can use else statement with while loop.
The pass Statement:
The pass statement in Python is utilized when an assertion is required grammatically yet you don’t need any order or code to execute.
The pass statement is a null operation; nothing happens when it executes. The pass is additionally valuable in spots where your code will at last go, yet has not been composed at this point (e.g., in stubs for instance):
Example:
#!/usr/bin/python for letter in 'Python': if letter == 'h': pass print 'This is pass block' print 'Current Letter :', letter print "Good bye!"
This will produce following result:
Current Letter : P Current Letter : y Current Letter : t This is pass block Current Letter : h Current Letter : o Current Letter : n Good bye!
The first code doesn’t execute any assertion or code if the worth of letter is ‘h’. The pass statement is useful when you have made a code block yet it is not, at this point required.
You would then be able to eliminate the assertions inside the square however let the square stay with a pass explanation so it doesn’t meddle with different pieces of the code.