Loops Notes
Loops Notes
What is a Loop?
In computer programming, a loop is used to execute a group of instructions or a block
of code multiple times without writing it repeatedly. The block of code is executed
based on a certain condition. Loops are the control structures of a program.
1. Control statement: This comprises the conditions that must be met for the
execution of the body of the loop. For every iteration of the loop, the conditions
in the control statement must be true.
2. Body: This comprises the block of code or the sequence of logical statements
that are to be executed multiple times.
When you use a loop in your program, you will not have to write the block of code
(written in the body of the loop) over and over again in it. The block of code will be
executed as many times as the control statement will hold true, and the loop will be
terminated when the conditions in the control statement become false.
If the conditions are not clearly defined in the control statement, the loop will keep on
executing. Such loops are termed infinite loops. If no termination condition is
provided in the control statement of a loop, then it automatically becomes an infinite
loop.
Types of Loops
There are basically two types of loops in most computer programming languages:
Page 1
Created by Turbolearn AI
There are two types of loops in Python: for loop, and while loop. When a loop is
written within another loop, the control structure is termed a nested loop.
Syntax:
for loop
A for loop is used for iterating over a sequence (that is either a list, a tuple, a
dictionary, a set, or a string).
Syntax:
Page 2
Created by Turbolearn AI
Examples:
for x in range(5):
print(x, end=" ")
# Output: 0 1 2 3 4
s = "Geeks"
for i in s:
print(i)
# Output:
# G
# e
# e
# k
# s
for i in range(1,20,2):
print(i, end=" ")
# Output: 1 3 5 7 9 11 13 15 17 19
Page 3
Created by Turbolearn AI
for i in range(2,20,2):
print(i, end=" ")
# Output: 2 4 6 8 10 12 14 16 18
n=int(input("Enter a number"))
sum=0
for i in range(1,n):
sum=sum+i
print(sum)
n=int(input("Enter a number"))
product=1
for i in range(1,n):
product=product*i
print(product)
while loop
A while loop is an entry controlled loop. The condition checking is done at the
beginning of the loop structure.
Syntax:
while(condition):
Initialize loop variable
Body of the Loop or statements to be repeated
increment/decrement loop variable
The condition checking is done before the execution of the body of the while loop.
The block of code in the body of the while loop is executed only if the condition is
true. The body of the loop gets executed as many times as the condition is true. After
each iteration of the loop, the control moves back to the condition checking part at
the beginning of the while loop. If the condition is not met, that is, if the boolean
expression in the braces (), turns out to be false, the while loop is terminated.
Example:
Page 4
Created by Turbolearn AI
n=5
i=1
while(i<=n):
print (i)
n+=1
# Output: 1 2 3 4 5
In the given example, the variable n is initialized as an integer, and its value is
assigned as 5 and loop variable i is initialized as 1. Every time the condition i<=n is
met, the while loop will be executed, and the value of i will be displayed on the
screen. At every iteration, the value of i will be increased by 1. The loop will be
terminated when the value of i becomes greater than 5. The above while loop will
display the numbers from 1 to 5.
count = 0
while True:
print(count, end=" ")
count += 1
if count >= 5:
break
# Output: 0 1 2 3 4
continueis used to skip the current block, and return to the "for" or "while"
statement.
Example:
for x in range(10):
if x % 2 == 0:
continue
print(x, end=" ")
# Output: 1,3,5,7,9
else statements
Page 5
Created by Turbolearn AI
else with loop is used with both while and for loop. The else block is executed at the
end of the loop, meaning when the given loop condition is false, then the else block
is executed. The else keyword in a for loop/while loop specifies a block of code to be
executed when the loop is finished.
Note: The else block will NOT be executed if the loop is stopped by a break
statement.
Examples:
Page 6