Friday, 20 July 2012

The break Statement of Python


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 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.

Example:

#!/usr/bin/python
For letter in ‘Python’ :               # First Example
            If letter  ==  ‘h’:
                Break
            Print  ‘Current  Letter  : ‘ ,  letter

var  = 10
while  var  > 0:
     print  ‘Current   Variable Value  :’, letter
     var  =  var -1
    if  var  ==  5:
            break;
print  ”Good bye !”

This will produce 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!

No comments:

Post a Comment