Friday, 20 July 2012

The else Statement Used with Loops


Python supports to have an else statement associated with a loop statements.

·         If the else statement is used with a for loop, the else statement is executed when the loop has exhausted iterating the list.
·         If the else statement is used with a while loop, the else statement is executed when the condition becomes false.


Example:

The following example illustrates the combination of an else statement with a for statement that searches 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 facto
               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.

The Continue Statement:


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:

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

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

This will produce following result:

Current Letter :  P
Current Letter :  y
Current Letter :  t
Current Letter :  h
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 :  5
Current Variable Value :  4
Current Variable Value :  3
Current Variable Value :  2
Current Variable Value :  1
Good bye!

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!

The else Statement of Python


An else statement can be combined with an if statement. An else statement contains the block of code that executes if the conditional expression in the if expression in the if expression resolves to 0 or a false value.

The else statement is an optional statement and there could be at most only one else statement following if.

The syntax of the if….else statement is:

If expression :
            Statement(s)
else:
            Statement(s)

Example:

# ! /usr/bin/python

var1 = 100
if var1:
            print “1 – Got a true expression value”
            print var1
else
            print “1 – Got a true expression value”
            print var1
var2 = 0
if var2:
            print “2 – Got a true expression value”
            print var2
else
            print “2 – Got a true expression value”
            print var2
print  “Good bye”


This will produce following result:

1 – Got a true expression value
100
2 – Got a true expression value
0
Good bye!

If statement of Python


The If statement of python is similar to that of other languages. The if statement contains a logical expression using which data is compared, and a decision is made based on the result of the comparison.
The syntax of the If statement is:

If expression:
    Statement (s)

Here if statement condition is evaluated first. If condition is true that is, if its value is nonzero then the statement(s) block are executed. Otherwise, the next statement following the statement(s) block is executed.

Note: In python, all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code.  Python uses indentation as its method of grouping statements.


Example:
# ! /usr/bin/python
var1 = 100
If  var1:
            Print “1 – Got a true expression value”’
            Print var1
Var2 = 0
If  var2:
            Print “2 – Got a true expression value”’
            Print var2
Print “Good bye!”

This will produce following result:

1 – Got a true expression value
100
Good bye!

The Nested if…elif…else Construct:


There may be a situation when you want to check for another condition after a condition resolves a condition resolves to true. In such a situation, you can use the nested if construct.

In a nested if construct, you can have an if…elif…else construct inside another if…elif…else construct.
The syntax of the nested if…elif…else construct may be:

If expression1:
            statement(s)
If expression2:
            statement(s)
elif expression3:
            statement(s)
else
            statement(s)
elif expression4:
            statement(s)
else:
            statement(s)

Example:

#!/usr/bin/python

Var = 100
If var < 200
  print “Expression value is less than 200”
if var == 150:
  print “which is 150”
elif var == 100:
   print “Which is 100”
elif  var == 50:
   print  “Which is 50”

elif var < 50:
    print  “Expression value is less than 50”
else:
   print  “could not find true expression ”

print  “Good bye!”

This will produce following result:

Expression value is less than 200
Which is 100
Good bye!

Single Statement Suites:


If the suite of an if clause consists only of a single line, it may go on the same line as the header statement:

Here is an example of a one-line if clause:

If(expression  ==  1)  :  print  “value of expression is 1”