0% found this document useful (0 votes)
1 views13 pages

Python Loops

Uploaded by

Vinod Srivastava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views13 pages

Python Loops

Uploaded by

Vinod Srivastava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Process Control-Loops

VKS-12

LOOPS
Iteration
VKS-12
VKS-12

 Repeated execution of a set of statements is called iteration


 Iteration is achieved in programing language using loop.
 The iteration can be definite or indefinite depending upon
the user logic
 All programming language provides some construct which are
used to automatically repeat a set of statements generally
called Loops
 Python provide two type of loops while and for
 while is condition-controlled loop
 for is count-control loop

For loops are traditionally used when you have a piece of code which
you want to repeat n number of times.
While Loop is used when a condition is to be met, or if you want a
piece of code to repeat forever,
VKS-12

Generally any loop in Python has three (3) components:


a) Control Variable and its Initialisation: Generally a loop in Python
has a control variable. Control variable is generally an integer type or
character type or floating point type. But we can have loop in Python
with out a control variable. We will discuss such loops later. If a loop
has a control variable then the control variable has to be initialized.
b) Terminating Condition: Generally a loop should terminate after
certain number of iterations. Terminating condition of the loop in
Python is a condition (logical expression) involving the Control
Variable. Condition or logical expression plays a very important in the
working of a loop in Python since number of times loop is repeated
depends on the condition.
c) Updation of Control Variable: Every Python loop repeats statement
or block. Inside the block or statement of a loop, control variable is
updated (value of the control variable is either incremented or
decremented), so that the loop terminates after certain number of
iterations.
VKS-12

Loop through a set of statements as long as a condition is true

Syntax
while(Condition) :
Python Statement(s)
Updating of control variable

The (condition) may be any Python valid logical expression.


When program execution reaches a while statement, the following events occur:
1. The (condition) is evaluated.
2. Condition is TRUE (nonzero), the Block / Statement is executed.
3. Inside the Block / Statement, control variable is updated and Condition is tested
once
These three steps are repeated till Condition is FALSE.
When the condition is evaluated to be false, the loop is terminates.
VKS-12

k<=5 print(k) k+=1


TRUE 1 2
TRUE 2 3
TRUE 3 4
TRUE 4 5
TRUE 5 6
FALSE Loop terminated
VKS-12

K<=20 print(k) K+=2


True 2 4
True 4 6
VKS-12

range()
Function range() generates a list of numbers, which
is generally used to iterate over with for loop. Often
you will want to use this when you want to perform
an action for fixed number of times.

Function range() has three syntaxes.


VKS-12

1. range(StopVal) generates sequence of


integers 0, 1, 2, 3, …, StopVal-1
Start value of the sequence is always 0 (zero)
when range() function has single parameter.
range(6) will generate sequence of integers
0, 1, 2, 3, 4, 5
range(11) will generate integers
0, 1, 2, 3, …, 9, 10
range(0) or range(-5) does not generate an any
sequence because 0>StopVal
VKS-12

2. range(StartVal, StopVal) generates integers


StartVal, StartVal +1, StartVal +2, , …, StopVal-1
range(1, 6) will generate sequence of integers
1, 2, 3, 4, 5
range(6, 11) will generate sequence of integers
6, 7, 8, 9, 10
range(-3, 4) will generate sequence of integers
-3, -2, -1, 0, 1, 2, 3
range(3, 0) or range(3, -4) does not generate an any
sequence of integer because StartVal>StopVal
VKS-12

3. range(StartVal, StopVal, Step)


a) Generates integers StartVal, StartVal + Step,
StartVal + 2*Step, …, StopVal-1
When StartVal<StopVal and Step>0
Generates a sequence in ascending order.

b) Generates integers StartVal, StartVal - Step,


StartVal - 2*Step, …, StopVal+1
When StartVal>StopVal and Step<0
VKS-12

Generates a sequence in descending order.


range(1, 6, 1) will generate sequence of integers 1, 2, 3, 4, 5
range(2, 12, 2) will generate sequence of integers 2, 4, 6, 8, 10
range(1, 12, 2) will also generate sequence of integers 1, 3, 5, 7, 9, 11
range(-1, -6, -1) will generate sequence of integers -1, -2, -3, -4, -5
range(15, 2, 3) will generate sequence of integers 15, 12, 9, 6, 3
range(-15, -2, 3) will generate sequence of integers -15, -12, -9, -6, -3
range(-5, 6, 2) will generate sequence of integers -5, -3, -1, 1, 3, 5
range(-9, 0, -2) does not generate an any sequence because
StartVal<StopVal and Step is -2

You might also like