Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

LOOPS

What is a Loop?
• This refers to statements in a program executed
repeatedly, either a fixed number of times or
until some condition is TRUE or FALSE.
• It also serves as control structures in which a
block of instructions is repeated until a condition
is fulfilled.
What is a Loop?

•In python, there are three looping


statements for, while and do-while. Each of
them has specific purpose in different
programming problems that a programmer
may encounter
The advantages of using looping statement

•Reduces the length of code


•Takes less memory space
For Loop Statement

•For loop in python requires to variables to


work. The first is the iterable object such as
list, a tuple or a string. Second is the
variable to store the successive values from
the sequence in the loop.
For Loop Statement Sytax
for iter in sequence:
statements (iter)

The “iter” represents the iterating variable. It gets


assigned with the successive values from the input
sequence.
The “sequence” may refer to any of the following
python objects such as a list, a tuple or a string.
Reasons to use a for loop statement

•Know the number of times the loop should


iterate.
•Using a counter
•Need a false condition to terminate the loop
Reasons to use a for loop statement

•Know the number of times the loop should


iterate.
•Using a counter
•Need a false condition to terminate the loop
While loop statement

In python programming language, the while


loop is used to execute a number of
statements or body till the specified
condition is true. Once the condition is false,
the control will come out of the loop.
While loop statement syntax

Syntax

While <expression>:
body

You might also like