Mindmajix

Loops

In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. There may be a situation when you need to execute a block of code several number of times.

Programming languages provide various control structures that allow for more complicated execution paths.

A loop statement allows us to execute a statement or group of statements multiple times. The following diagram illustrates a loop statement −

Loop Architecture

Loops are required when there is a need to execute a piece of code one or more times. Just like any other language, python too supports loops. Various types of loops can be defined as follows:

-While loop :

Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body. It helps in repeating a block of code till a particular condition holds true.

Format:

while condition:

code_to_execute

EXAMPLE:

a=0

while a<10:

print a

a=a+1

Output: 0;1;2;3;4;5;6;7;8;9

-For loop:

Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. It uses a loop variable to repeat a block of code. Here are some points that you must keep in mind while working with for loop:

  1. It must reference a list or range
  2. For loop closes with a colon
  3. The code that needs to be executed must be indented by one tab space

Format:

For condition:

Code_to_execute

EXAMPLE

a=[‘Sunday’,’Monday’,’Tuesday’,’Wednesday’,’Thursday’,’Friday’,’Saturday’] for item in a:

print item

Output: Sunday;Monday;Tuesday;Wednesday;Thursday;Friday;Saturday

-Nested loops

You can use one or more loop inside any other “while, for or do..while loop.” They are used in a scenario where there is a need to use one loop inside the other.

Format

for condition1:

for condition2:

code_to_execute2

code_to_execute1

or

while condition1:

while condition2:

code_to_execute2

code_to_execute1

EXAMPLE

Nested for loop

i=[‘a’,’b’,’c’]

j=[‘d’,’e’,’f’]

k=[‘g’,’h’,’i’]

l=[i,j,k]

m=1

for item in l:

print m

for element in item:

print element

m=m+1

Output:

1;a;b;c;2;d;e;f;3;g;h;i

-Nested While loop

x=1

y=10

z=20

while x<y:

print ‘x= ‘,x

print ‘x is less than y’

x=x+5

while y<z:

print ‘y= ‘,y

print ‘y is greater than x but less than z’

y=y+4

Output:

x= 1; x is less than y; y= 10; y is greater than x but less than z; y= 14; y is greater than x but less than z; y= 18; y is greater than x but less than z; x= 6; x is less than y; x= 11; x is less than y; x= 16; x is less than y; x= 21; x is less than y.

Normal execution of loops can be controlled with the help of control statements. The different types of control statements are described below:

-Break statement:

Terminates the loop execution and jumps to statement just after the loop

Format:

break

EXAMPLE:

x=1

while x<11:

if(x%2==0):

print ‘x is even ‘,x

else:

print ‘x is odd’,x

x=x+1

if(x==5):break

print ‘I will not go further’

Output:

x is odd 1;x is even 2;x is odd 3;x is even 4;I will not go further

-Continue Statement :

Skips the execution of remaining statements of the loop and jumps to the loop’s header line.

Format:

continue

EXAMPLE:

x = 10

while x:

x=x-1

if x%2!=0: continue

print(x,’is even and less than 10′)

Output:

(8, ‘is even and less than 10’); (6, ‘is even and less than 10’); (4, ‘is even and less than 10’);(2, ‘is even and less than 10’);(0, ‘is even and less than 10’)

Pass Statement :

The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.

Format :

pass.

EXAMPLE :

        #!/usr/bin/python

        for letter in ‘Python’:

        if letter == ‘h’:

        pass

        print ‘This is pass block’

        print ‘Current Letter :’, letter

        print “Good bye!”

 

Enroll for Instructor Led Live Python Training


0 Responses on Loops"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.