0% found this document useful (0 votes)
10 views21 pages

Python Iteration and Looping Explained

Uploaded by

believezhou200
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)
10 views21 pages

Python Iteration and Looping Explained

Uploaded by

believezhou200
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

3.

ITERATION OR LOOPING

ITERATION
3. ITERATION OR LOOPING

What is loop or iteration?


Loops can execute a block of code number
of times until a certain condition is met.
OR
The iteration statement allows instructions to be
executed until a certain condition is to be
fulfilled.
The iteration statements are also called as
loops or Looping statements.
3. ITERATION OR LOOPING

Python provides two kinds of loops


& they are,

while loop

for loop
while loop

A while loop allows general repetition


based upon the repeated testing of a Boolean
condition
The syntax for a while loop in Python is as
follows:
while condition: : Colon Must
body
Where, loop body contain the single
statement or set of statements (compound
statement) or an empty statement.
Contd..
while loop - programs
# Natural Numbers generation

OUTPUT
for LOOP

Python’s for-loop syntax is a more


convenient alternative to a while loop when
iterating through a series of elements. The for-
loop syntax can be used on any type of iterable
structure, such as a list, tuple str, set, dict, or
file
Syntax or general format of for loop is,

for element in iterable:


body
for LOOP

Python’s for-loop syntax is a more


convenient alternative to a while loop when
iterating through a series of elements. The for-
loop syntax can be used on any type of iterable
structure, such as a list, tuple str, set, dict, or
file
Syntax or general format of for loop is,

for element in iterable:


body
for LOOP

Till the list


exhaust for loop
will continue to
execute.

OUTPUT
for LOOP - range KEYWORD

The range() function returns a


sequence of numbers, starting from 0 by
default, and increments by 1 (by default),
and ends at a specified number.

range(start, stop, step)

x = range(3, 6)
for n in range(3,6):
OR for n in x:
print(n)
print(n)
for LOOP - range KEYWORD

#Generating series of numbers

OUTPUT
for LOOP - range KEYWORD
#Generating even numbers

OUTPUT
for LOOP - range KEYWORD
# print string character by character

OUTPUT
else statement in loop

else can be used in for and while loops the


else body will be executed as and when the
loop’s conditional expression evaluates to false
OUTPUT
4. BRANCHING OR JUMPING STATEMENTS

1. break STATEMENT

Break can be used to unconditionally


jump out of the loop. It terminates the
execution of the loop. Break can be used in
while loop and for loop. Break is mostly
required, when because of some external
condition, we need to exit from a loop.
1. break STATEMENT

Loop
Condition ? break
causes
jump
True

Statement 1

break
1. break STATEMENT

OUT PUT
2. continue STATEMENT

The continue statement in


Python returns the control to the
beginning of the while loop. The continue
statement rejects all the
remaining statements in the current
iteration of the loop and moves the
control back to the top of the loop.
The continue statement can be used in
both while and for loops.
2. continue STATEMENT

Loop False
Condition ?

True
Statement 1

Statements
ignored or continue
skipped
continue
Statement n
causes
jump
2. continue STATEMENT

when i value becomes 2 the print statement gets


skipped, continue statement goes for next iteration,
hence in the out put 2 is not printed
Difference Between break and continue

break continue
Difference Between break and continue
BREAK CONTINUE
It terminates the execution It terminates only the current
of remaining iteration of iteration of the loop.
the loop.
'break' resumes the control 'continue' resumes the control
of the program to the end of the program to the next
of loop enclosing that iteration of that loop enclosing
'break'. 'continue'.
It causes early termination It causes early execution of the
of loop. next iteration.
'break' stops the 'continue' do not stops the
continuation of loop. continuation of loop, it only
stops the current iteration.

Common questions

Powered by AI

The 'continue' statement, when used in a loop, causes the current iteration to end immediately and control to jump to the beginning of the next iteration. This can affect the loop's overall logic by preventing the execution of certain statements within specific iterations, effectively skipping them, rather than executing the loop normally where every statement in the loop body runs on every iteration .

The 'break' statement terminates the execution of the entire loop, resuming control to the end of the loop, while the 'continue' statement only terminates the current iteration of the loop, resuming control to the next iteration within the same loop. The 'break' statement stops the loop's continuation, whereas 'continue' merely skips to the next iteration without stopping the loop completely .

Nested for-loops can be used effectively for tasks requiring multi-level iterative operations, such as iterating over a matrix or processing multi-dimensional data. Key considerations include managing performance, as nested loops can increase time complexity, and ensuring clear control flow and variable management to avoid confusion. Typically, break and continue statements should be used carefully to maintain logical flow within nested structures .

Iteration statements, or loops, in Python allow the execution of a block of code multiple times until a condition is met. They are primarily categorized as 'for' and 'while' loops. 'For' loops iterate over elements of an iterable, while 'while' loops execute as long as a condition remains true. The choice between them depends on whether you need general repeating execution (while loops) or to iterate over a collection (for loops).

The 'range()' function enhances a 'for' loop by generating a sequence of numbers, providing a controlled iteration count. It accepts parameters for start, stop, and step to define the sequence. For example, 'for n in range(3, 6): print(n)' iterates over numbers 3 through 5, allowing precise control over loop iteration without manually indexing .

To iterate over characters in a string using a 'for' loop in Python, you utilize the syntax 'for char in string: body', where each iteration accesses a character from the string in sequence. This loop structure is the most appropriate as it directly accesses each element (character) within the iterable structure (string).

A 'break' statement is used inside a loop to immediately terminate the loop's execution and exit to the following statement. It is beneficial when an external condition requires exiting the loop prematurely, such as when a certain condition is met that renders further iterations unnecessary or irrelevant, like finding a target in a search .

The 'else' statement following a loop in Python is executed when the loop's conditional expression evaluates to false, meaning the loop completes normally without encountering a 'break' statement. This can be used to check for non-abnormal termination conditions, offering a way to execute a set of statements if the loop completes without interruption .

A 'while' loop might be chosen over a 'for' loop in scenarios where the number of iterations cannot be determined before execution, often based on dynamic conditions. Factors influencing this decision include whether repetition depends on a continually evaluated condition (favoring 'while'), or whether iterating a fixed range or collection is required (favoring 'for').

A 'while' loop in Python uses the syntax 'while condition: body', relying on a Boolean condition to determine continuation. It is typically used for general repetition based on a condition. In contrast, a 'for' loop uses the syntax 'for element in iterable: body', iterating over elements of an iterable structure like lists or tuples. 'For' loops are commonly used when the number of iterations is known or when iterating over a collection .

You might also like