0% found this document useful (0 votes)
7 views35 pages

Ch5. Loop Control Statements: Dr. Tulika Assistant Professor Department of Computer Science Miranda House

This document provides an overview of loop control in Python, detailing the two primary types of loops: while and for loops. It explains the syntax and usage of while loops, the range() function for generating integer sequences, and the structure of for loops, including nested loops. Additionally, it covers the break and continue statements that control loop execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views35 pages

Ch5. Loop Control Statements: Dr. Tulika Assistant Professor Department of Computer Science Miranda House

This document provides an overview of loop control in Python, detailing the two primary types of loops: while and for loops. It explains the syntax and usage of while loops, the range() function for generating integer sequences, and the structure of for loops, including nested loops. Additionally, it covers the break and continue statements that control loop execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

CH5. LOOP CONTROL Dr.

Tulika
Assistant Professor

STATEMENTS Department of Computer Science


Miranda House
5.1 INTRODUCTION
Loops are used to repeat the same code multiple times.
Python provides two types of loop statements, viz. while and for loops. The while loop
is a condition controlled loop. It is controlled by true or false conditions.
The for loop is a count controlled loop which repeats for a specific number of times.
5.2 THE WHILE LOOP
The while loop is a loop control statement in Python and frequently used in
programming for repeated execution of statement(s) in a loop.
It executes a sequence of statements repeatedly as long as a condition remains true.
The syntax for while loop is given as follows:
while test-condition:
#Loop Body
statement(s)
The above code is called bad code because the entire loop body must be
indented inside the loop.
Since the statement count=count+1 is not in the loop body, the loop executes
for infinite number of times. And because the value of count is always 0, the
condition count <=5 is always true.
5.3 THE RANGE() FUNCTION
range() is used to generate a list of integers.
The range function has one, two or three parameters. The last two parameters in
range() are optional.
The general form of the range function is: range(begin, end, step)
The ‘begin’ is the first beginning number in the sequence at which the list starts.
The ‘end’ is the limit, i.e. the last number in the sequence.
The ‘step’ is the difference between each number in the sequence.
Example 1
Create a list of integers from 1 to 5.
>>> list(range(1,6))
[1,2,3,4,5]
range(1,6) function is used in the above example. It generates a list of integers
starting from 1 to 5. Note that the second number, i.e. 6 is not included in the elements
of this list.
Example 2
Create a list of integers from 1 to 20 with a difference of 2 between two successive
integers.
>>> list(range(1,20,2))
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
5.4 FOR LOOP
for var in sequence:
statement(s)

5.4.1 Details of for Loop


The keywords for and in are essential keywords to iterate the sequence of values.
The variable var takes on each consecutive value in the sequence and the statements
in the body of the loop are executed once for each value.
The variable var takes on each consecutive value in the sequence and the statements
in the body of the loop are executed once for each value. A simple example of for
loop is:
for var in range(m,n):
print var
The function range(m, n) returns the sequence of integers starting from m, m+1, m+2,
m+3…………… n-1.
5.5 NESTED LOOPS
Loops within the loops or when one loop is inserted completely within another loop,
then it is called nested loop.
5.6 THE BREAK STATEMENT
The keyword break allows a programmer to terminate a loop. When the break
statement is encountered inside a loop, the loop is immediately terminated and the
program control automatically goes to the first statement following the loop.
5.7 THE CONTINUE STATEMENT
The continue statement is exactly opposite of the break statement. When continue is
encountered within a loop, the remaining statements within the body are skipped but
the loop condition is checked to see if the loop should continue or exit.

You might also like