Tuesday, 21 March 2017

Python: Looping Statemnts

Looping Statements in Python:

Looping statements are used to execute the set of statements Repeatedly.

Python supports 2 types of Loops, they are
  • while loop
  • for loop
While Loop:
While loops executes the set of statements are repeatedly until condition will become False.

                                          (or)

While loops executes the repeatedly until while condition is False.

syntax 1:
                           while  condition :  statement
syntax 2:
                           while condition:
                                           statement 1
                                           statement 2
                                           --------------
                                          --------------
                                         statement n
Example 1:
i=1
sum=0
while i<=100:
    sum=sum+i
    i=i+1
print(sum)

Output:
5050

Example 2:
a=0
while a<5:
    print(a)
    a+=1

Output:
0
1
2
3
4
While loop with else:
syntax:
                         while  condition:
                                            statement 1
                                            statement 2
                                            ---------------
                                            ---------------
                                            statement n
                        else:
                              statement 1
                              statement 2
                              ---------------
                              ---------------
                              statement n

Infinite while loop:
  • Executing no.of times keep on upto sufficient memory.
  • in order to work with the infinite loops,we are using break statement
syntax:
                             while  True:
                                          -------------
                                          -------------
                                          -------------
                                          -------------

Example 1:
i=1
while True:
    print("hai siva")
    if i==5:
            i=i+1
Output:
hai siva
hai siva
hai siva
hai siva
hai siva
hai siva
hai siva
hai siva
hai siva
hai siva
hai siva
hai siva
............
...........
............
............
Traceback (most recent call last):
  File "E:/python_practice/examples/infinite while.py", line 3, in <module>
    print("hai siva")
  File "C:\Program Files\Python35\lib\idlelib\PyShell.py", line 1344, in write
    return self.shell.write(s, self.tags)
Keyboard Interrupt

Note:
solve the problems in infinite loops, we are going to use break statement

Example 2:
i=1
while True:
    print("hai siva")
    if i==5:
        break
    i=i+1

Output:
hai siva
hai siva
hai siva
hai siva
hai siva

break statement:
  • break statement is used in the looping statements.
  • when ever break statement of the loop is executed an automatically control will come out from the loop.
  • Generally we use the break statement to exit from the infinite loops.
Example:
i=1
while True:
    print("hai siva")
    if i==5:
        break
    i=i+1
else:
    print("hai krishna")

Output:
hai siva
hai siva
hai siva
hai siva
hai siva

  • we can use the else block with the while loop in python,when ever while loop condition becomes False,then only control will goto the else block.
  • if we are exiting from the while loop,by executing break statement when control will not go to the else block.
continue statement:
  • continue statements are used in loops.
  • skipping the particular iteration
  • when ever continue statement of the loop is executed can without executing the remaining part of that particular iteration when control will goto the next iteration.
Example:
i=0
while i<5:
    i=i+1
    if i==2:
        continue
    print("hai siva",i)
else:
    print("hai krishna")

Output:
hai siva 1
hai siva 3
hai siva 4
hai siva 5
hai krishna

using continue and break statements in a single program

 Example: Login form

while True:
    name=input("enter UserName: ")
    if name!= 'siva':
        continue
    password=input("Helo siva, enter your PassWord ?  ")
    if password=='krishna@143':
        break
print("Access Granted")

Output:
enter UserName: siva
Helo siva, enter your PassWord ?  krishna@143
Access Granted

Output 2:
enter UserName: abc
enter UserName: xyz
enter UserName: siva
Helo siva, enter your PassWord ?  krishna
enter UserName: siva
Helo siva, enter your PassWord ?  krishna@142
enter UserName: siva
Helo siva, enter your PassWord ?  krishna@143
Access Granted

Note:
  • Here continue and break statements are written in the if conditions,but both are working based on loops.
for  loop:
  • for loop executes the set of statements with respect to every element of the collection object.
Syntax:
                               for  variablename  in collvariable :
                                                               ----------------------
                                                               ----------------------
                                                               ----------------------
Example 1:
x=[10,20,30,40,50]
for p in x:
    print(p)

Output:
10
20
30
40
50

Example 2:
x="sivakrishna"
for p in x:
    print(p)

Output:
s
i
v
a
k
r
i
s
h
n
a

Note:
  • for loop takes string object also,but string object is not a collection object.
  • string object is a group of  characters.
Some times We are not interested blocks, simply that blocks are passed by using pass keyword. 

Example:
                if  condition :
                          pass


No comments:

Post a Comment