Loops
Loops
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.
What is a Loop
Examples:
for x in range(5): for x in range(3, 6): for x in range(3, 8, 2):
print(x, end=“ ”) print(x, end=“ ”) print(x, end=“ ”)
Output: Output:
Apple G
Banana e
cherry e
k
s
for loop
WAP to display first ‘n’ numbers
n=int(input(“Enter upto which no:”))
for i in range(n):
print(i)
Output:
If n is 5, then
1
2
3
4
5
for loop
WAP to display all odd numbers below 20
for i in range(1,20,2):
print(i, end=“ ”)
Output:
1 3 5 7 9 11 13 15 17 19
for loop
WAP to display all even numbers below 20
for i in range(2,20,2):
print(i, end=“ ”)
Output:
2 4 6 8 10 12 14 16 18
for loop
WAP to display sum of first n numbers
n=int(input(“Enter a number”)
sum=0
for i in range(1,n):
sum=sum+i
print(sum)
for loop
WAP to display factorial of n (product of first n
numbers)
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. The
general syntax of the while Loop is given below.
Initialize loop variable
while(condition):
Body of the Loop or statements to b e repeated
increment/decrement loop variable
while loop
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.
while loop
Example: In the given example, the variable ‘n’ is
n=5 initialized as an integer, and its value is
i=1
while(i<=n): assigned as 5 and loop variable i is
print (i) initialized as 1. Every time the condition
n+=1 ‘i<=n’ is met the While Loop will be
Output: executed, and the value of i will be
1 displayed on the screen. At every
2 iteration, the value of i will be increased
3 by 1. The Loop will be terminated when
the value of ‘i’ becomes greater than 5.
4
The above While Loop will display the
5 numbers from 1 to 5.
break & continue statements
break is used to exit a for loop continue is used to skip the
or a while loop. current block, and return to
the "for" or "while" statement.
Example:
count = 0 Example:
while True: for x in range(10):
print(count, end=“ ”) if x % 2 == 0:
count += 1 continue
if count >= 5: print(x, end=“ ”)
break
Output: Output:
01234 1,3,5,7,9
else statements
else with loop is used with both while and for loop. The else
block is executed at the end of loop means 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.
• So let’s see the example of while loop and for loop with else
below.
else statements-examples