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/24/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/24/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
Dept of IT - Jagannadham Ch 1/24/2026 4
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/24/2026 5
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/24/2026 6
Flowchart and Example
Dept of IT - Jagannadham Ch 1/24/2026 7
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.
o Default step value is 1.
o The step value can be positive or negative, but not zero.
• Forms of range()
o If range() is given single argument ,it produce an object values from 0 to argument-1
o If range() is given two arguments , it produce an object values from beg to end .
o If range() is given three arguments ,then third arguments specifies the interval of the sequence.
Dept of IT - Jagannadham Ch 1/24/2026 8
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: - Print a simple pattern
Dept of IT - Jagannadham Ch 1/24/2026 9
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/24/2026 10
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/24/2026 11
• 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/24/2026 12
Thank ‘U’