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

Python2 1 LoopBreakContinue

The document discusses loop control structures in Python, explaining the concept of looping and its necessity in programming. It details the two main types of loops: 'while' and 'for', along with their syntax, usage, and examples, including nested loops and infinite loops. Additionally, it covers the use of 'else' with loops, practical applications, and provides practice problems for better understanding.

Uploaded by

mayipathivada09
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)
13 views21 pages

Python2 1 LoopBreakContinue

The document discusses loop control structures in Python, explaining the concept of looping and its necessity in programming. It details the two main types of loops: 'while' and 'for', along with their syntax, usage, and examples, including nested loops and infinite loops. Additionally, it covers the use of 'else' with loops, practical applications, and provides practice problems for better understanding.

Uploaded by

mayipathivada09
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

Loops or Iterative Control

Structures
- By Jagannadham Ch
Loop Control Structures
• In earlier programs, each instruction was executed only once.
• However, many programs require a group of instructions to be executed repeatedly until a specific condition is
satisfied, This process is known as looping or iteration.
• A loop is a control structure that allows the computer to execute a set of statements repeatedly, either a fixed number of
times or while a condition remains true.
• The repetition continues as long as the condition is true.
• When the condition becomes false, the loop terminates and control passes to the statement (if any) following the loop.
• A loop consists of two main parts:
o Control statement – checks the condition
o Body of the loop – contains the statements to be repeated
• Types of Loop Control Structures
• In Python, there are two main loop control structures:
o while loop
o for loop
o Note: The do–while loop exists in C language but is not used in Python.

Dept of IT - Jagannadham Ch 1/28/2026 2


While Loop Control Structures
• The while loop is generally used when the number of iterations are not known in advance.
• The while loop repeats one or more statements as long as a given condition is True.
• Syntax of while Loop

• Important Points
o The condition is tested before executing the loop body.
o If the condition is True, the statements inside the loop are executed.
o If the condition is False, the loop terminates and control moves outside the loop.
• Example: Print numbers from 1 to 10 using while loop
o Explanation:
Initially, i = 1, which satisfies the condition i <= 10.
The value of i is printed and then incremented.
This continues until i becomes 11, after which the condition becomes false and the loop stops.

Dept of IT - Jagannadham Ch 1/28/2026 3


Nested While Loops
• When one while loop is placed inside another while loop, it is called nested while loops.
• Factorial of first n natural numbers using nested while loop – Output

Dept of IT - Jagannadham Ch 1/28/2026 4


Printing patterns using a while
• We can generate a line of stars and a column of stars using a single while loop
• How to generate Right-angle triangle pattern :
o Logic
▪ Outer while → rows
▪ Inner while → columns

• Write a python program to generate the followings patterns

Dept of IT - Jagannadham Ch 1/28/2026 5


Infinite loop using while
• An infinite loop is a loop that keeps running forever because its stopping condition
is never met (or there is no condition at all).
• Usually this happens by mistake… sometimes it’s done on purpose (like in servers or
games)
• Condition is always True :
o True never becomes False, so the loop runs forever.
• Condition never changes :
o i is always 1, so i > 0 is always true → infinite loop.
• Common mistake causing infinite loop : forgot to increase
o i never changes, so the condition is always true.

Dept of IT - Jagannadham Ch 1/28/2026 7


while with else
• In Python, a while – else statement is a bit unique and very useful once you get it
• Syntax: The else block runs only if the loop finishes WITHOUT break

• while with else (no break) :


o The loop condition becomes false → else executes
• while with break (else will NOT run) :
o else is skipped because of break

Dept of IT - Jagannadham Ch 1/28/2026 8


Real-life style example (searching)
➢ Since 25 is not in the list, loop ends normally → else runs.

• Important points to remember


o else runs only when loop ends naturally
o else does NOT run if:
▪ break is used
▪ program exits
o Works the same way with for–else too

Dept of IT - Jagannadham Ch 1/28/2026 9


Practice Problems
• Write a program to calculate the sum and average of the first 100 natural numbers using a while
loop.
• Write a program to print 30 asterisks (*) in a horizontal line.
• Write a program to calculate the sum and average of numbers from m to n using a while loop.
• Write a program to read numbers until -1 is encountered. Also count the number of positive,
negative, and zero values, and find the sum and average of positive and negative numbers.
• Write a program to find the sum of even numbers and the sum of odd numbers from m to n
using a while loop.
• Write a program to check whether a given number is an Armstrong number or not.
• Write a program to check whether a given number is perfect number or not
• Write a program to convert a decimal number to binary and a binary number to decimal.
• Write a program to find the Greatest Common Divisor (GCD) of two positive integers using a
while loop.

Dept of IT - Jagannadham Ch 1/28/2026 10


For Loop Control Structure
• The for loop is used to repeat a statement or group of statements a fixed number of times.
• It is generally used when the number of iterations is known in advance.
• In Python, the for … in statement is used to iterate over:
o sequences (list, tuple, string)
o other iterable objects
• This process is called traversal.
• Syntax of for Loop
• Key Features
o The sequence is specified only once.
o Values from the sequence are assigned to the loop variable one by one.
o The loop continues until the last element of the sequence is reached.
o Indentation is used to separate the loop body from the rest of the program.
Dept of IT - Jagannadham Ch 1/28/2026 11
Flowchart and Example

Dept of IT - Jagannadham Ch 1/28/2026 12


The range() Function
• The range() function is a built-in function in Python used to generate a sequence of
numbers.
• Syntax :
range(beg , end,[step])
• Characteristics
o The sequence starts from begin and ends at end - 1.
o The step argument is optional and default step value is 1.
o The step value can be positive or negative, but not zero.
• Forms of range()
Form Syntax Example

single parameter range(stop) range(5) → 0 to 4

double parameters range(start, stop) range(2, 6)

Triple parameters range(start, stop, step) range(1, 10, 2)

Dept of IT - Jagannadham Ch 1/28/2026 13


• Forms of range() Examples :

• Explanation
o range(10) generates numbers from 0 to 9
o range(1, 10) generates numbers from 1 to 9
o range(1, 10, 2) generates numbers from 1 to 9 with a step of 2

Dept of IT - Jagannadham Ch 1/28/2026 14


Nested For Loops
• When a for loop is placed inside another loop, it is called nesting of for loops.
o The inner and outer loops can be of the same or different types.
o One loop must be completely inside the other.
o Similar to nested if statements, loops can also be nested.
• Syntax :

• Example: -

Dept of IT - Jagannadham Ch 1/28/2026 15


Infinite Loop using for in Python
• An infinite loop is a loop that never stops executing unless it is forcibly terminated (for example, by
pressing Ctrl + C)
• Normally, for loops run for a fixed number of times, but they can become infinite if we use them
incorrectly.
• Infinite for loop using [Link]()
o [Link](1) generates numbers endlessly: 1, 2, 3, 4, ...
o The loop never ends → infinite loop
• Infinite loop with range()
o This loop runs for an extremely long time
o Practically behaves like an infinite loop
• With iter() and int type
o iter(int, 1) keeps calling int() which always returns 0
o Condition 0 != 1 is always true → infinite loop
• Note : Infinite loops can freeze your program if not handled properly.

Dept of IT - Jagannadham Ch 1/28/2026 16


for Loop with else
• In Python, a for loop can have an else block.
o The else block executes only if the loop completes normally
o The else block does NOT execute if the loop is terminated by break
• Syntax :

• for–else without break:

• for–else with break:

Dept of IT - Jagannadham Ch 1/28/2026 17


Practical Use – Checking Prime Number
• Here:
o The for loop checks the divisibility of a given number.
o The break statement is not executed, which causes the
else block to run.
o This is a classic real-world use of the for–else construct.

Dept of IT - Jagannadham Ch 1/28/2026 18


Practice Problems
• Write a program to calculate the sum and average of the first 100 natural numbers using a
for loop.
• Write a program to print all even numbers between 1 and 20.
• Write a program to print the multiplication table of a given number n.
• Write a program to display the squares, square roots, cubes, and cube roots of numbers
from 1 to 10.
• Write a program to find the factorial of a given number using a for loop.
• Write a program to find the maximum number from a given set of numbers.
• Write a program to generate the first n Fibonacci numbers, where n is entered by the user.
• Write a program to generate all prime numbers up to n, where n is entered by the user.
• Write a program to compute the value of pow(x, n).
Dept of IT - Jagannadham Ch 1/28/2026 19
Unconditional (Jump) Control Structures
• In some situations, the flow of control may be transferred without testing any
condition.
• While executing loops, it may be necessary to:
o exit the loop immediately
o skip the remaining statements of the loop
• Python provides the following jump statements: break , continue , pass
• The break Statement
o Terminates the loop immediately, it can be used in both for and while loops.

Dept of IT - Jagannadham Ch 1/28/2026 20


• The continue Statement
o Skips the remaining statements in the current iteration.
o Transfers control to the next iteration of the loop.

• The pass Statement


o A do-nothing statement.
o Used when a statement is syntactically required but no action is needed.

Dept of IT - Jagannadham Ch 1/28/2026 21


Thank ‘U’

You might also like