0% found this document useful (0 votes)
8 views3 pages

Loops_and_Iteration

The document discusses loops and iteration in programming, focusing on 'for' and 'while' loops for executing code repeatedly. It explains loop control statements like 'break' and 'continue', as well as the 'range()' function for generating sequences of integers. Example codes demonstrate the functionality of these concepts in practical scenarios.

Uploaded by

kumarbojja02
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)
8 views3 pages

Loops_and_Iteration

The document discusses loops and iteration in programming, focusing on 'for' and 'while' loops for executing code repeatedly. It explains loop control statements like 'break' and 'continue', as well as the 'range()' function for generating sequences of integers. Example codes demonstrate the functionality of these concepts in practical scenarios.

Uploaded by

kumarbojja02
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/ 3

Loops and Iteration

Loops and Iteration: Loops are used to execute a block of code repeatedly. They
are essential for iterating over data structures or for performing repetitive tasks.
Let us discuss types of Loops and iterations:

for loop: The for loop is used to iterate while loop: The while loop continues
over a sequence (e.g., lists, strings) and execution while a specified condition
execute a block of code for each item in remains True.
the sequence.

Example Code: Example Code:


string = 'Data' count = 1
for char in string: while count <= 3:
print(char) print(count)
count += 1
Output:
D Output
a 1
t 2
a 3

This code defines a string "Data" and The code initializes count to 1, then uses
then uses a for loop to iterate through a while loop to print values from 1 to 3
each character in the string, printing (inclusive), incrementing count with
each character one at a time. each iteration, resulting in the output "1,
2, 3".

Loop control statements (break and Nested loops: Nested loops involve
continue): using loops within loops for complex
● break is used to exit a loop iterations.
prematurely.
● continue is used to skip the rest of
the current iteration and proceed to
the next one.

1|P a g e
Example Code: Example Code:
# Using 'break' to exit the loop
prematurely i = 1
count = 1 while i <= 2:
while count <= 5: j = 1
print("Current count:", while j <=2:
count)
print(i, "x", j, "=", i *
if count == 3: j)
break # Exit the loop j += 1
when count is 3
i += 1
count += 1
print('\n')

# Using 'continue' to skip an


iteration
count = 1
while count <= 5:
if count != 3:
print("Current count:",
count)
count += 1

Output:
Current count: 1
Current count: 2 Output:
Current count: 3 1 x 1 = 1
Current count: 1 1 x 2 = 2
Current count: 2
Current count: 4 2 x 1 = 2
Current count: 5 2 x 2 = 4
The first part of the code uses break to This code prints a custom
exit the loop when count is 3, and the multiplication table pattern for
second part uses continue to skip numbers 1 and 2, displaying the results
printing when count is 3. in a 2x2 grid.

2|P a g e
Range Function
The range() function generates a sequence of integers, commencing at 0 by
default and incrementing by 1, concluding before a specified value.

Example Codes:

for i in range(5): for i in range(5,8): for i in range(-5,0):


print(i) print(i) print(i)

Output: Output: Output:


0 5 -5
1 6 -4
2 7 -3
3 -2
4 -1

In this code range() The range() function The range() function


function is used to create creates a sequence of creates a sequence of
a sequence of numbers numbers starting from 5 numbers starting from -5
from 0 to 4, and the loop and ending before 8. The and ending before 0. The
iterates through this loop prints each of these loop thus prints numbers
sequence, printing each numbers, including 5, 6, till -1.
number. and 7.

3|P a g e

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy