0% found this document useful (0 votes)
2 views6 pages

Loops Notes

The document explains the concept of loops in computer programming, which allow for the repeated execution of a block of code based on a condition. It details two main types of loops: entry controlled (for and while loops) and exit controlled (do...while loops), along with examples of their usage. Additionally, it covers the range() function, break and continue statements, and provides several programming exercises involving loops.

Uploaded by

vedant21310
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)
2 views6 pages

Loops Notes

The document explains the concept of loops in computer programming, which allow for the repeated execution of a block of code based on a condition. It details two main types of loops: entry controlled (for and while loops) and exit controlled (do...while loops), along with examples of their usage. Additionally, it covers the range() function, break and continue statements, and provides several programming exercises involving loops.

Uploaded by

vedant21310
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/ 6

Created by Turbolearn AI

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.

A loop allows you to repeat a block of code as long as a condition is true.

The structure of a loop can be divided into two parts:

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

Entry Controlled Loop:


In an entry controlled loop, the control statement is written right at the
beginning of the loop. This type of loop is also called a pre-checking loop.
The conditions in the control statements are checked at first, and only if
the conditions are true, the body of the loop is executed. If the condition
turns out to be false, the lines of code in the body of the loop will not be
executed.
Examples: for loop & while loop.
Exit Controlled Loop:
In an exit controlled loop, the control statement is written at the end of
the loop structure.
The lines of code in the body of the loop are executed once before the
condition is checked. Hence, this type of loop is also called a post-
checking loop.
Example: do… while loop

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.

The range() function


The range() function returns a sequence of numbers, starting from 0 by default, and
increments by 1 (by default), and stops before a specified number.

Syntax:

range(start, stop, step)

Start (Optional): An integer number specifying at which position to start.


Default is 0.
Stop (Required): An integer number specifying at which position to stop (not
included).
Step (Optional): An integer number specifying the incrementation. Default is 1.

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

for loopvar in range(argument/aguments):


statements to be repeated

Examples:

for x in range(5):
print(x, end=" ")
# Output: 0 1 2 3 4

for x in range(3, 6):


print(x, end=" ")
# Output: 3 4 5

for x in range(3, 8, 2):


print(x, end=" ")
# Output: 3 5 7

More Examples with list & string:

fruits = ["apple", "banana", "cherry"]


for x in fruits:
print(x)
# Output:
# Apple
# Banana
# cherry

s = "Geeks"
for i in s:
print(i)
# Output:
# G
# e
# e
# k
# s

Example: 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

Example: WAP to display all even numbers below 20

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

Example: 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)

Example: 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.

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.

break & continue statements


break is used to exit a for loop or a while loop.
Example:

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:

for i in range(1, 4):


print(I, end=" ")
else:
# Executed because no break in for
print("No Break")
# Output: 1 2 3 No Break

for i in range(1, 4):


print(i)
break
else:
# Not executed as there is a break
print("No Break")
# Output: 1

Programs using loops


1. WAP to display first ‘n’ numbers.
2. WAP to display odd numbers below ‘n’.
3. WAP to display first ‘n’ odd numbers.
4. WAP to display even numbers below ‘n’.
5. WAP to display first ‘n’ even numbers.
6. WAP to display the sum of first ‘n’ numbers.
7. WAP to display the factorial of ‘n’.
8. WAP to display all numbers in a range.
9. WAP to display numbers below ‘n’ in reverse order.
10. WAP to reverse a numbers.
11. WAP to find the sum of digits of a number.
12. WAP to display the multiplication table of ‘n’ for ‘m’ rows.
13. WAP to find whether the given number is a prime or not.
14. WAP to find whether the given number is a perfect number or not.
15. WAP to find whether the given number is an Armstrong number or not.

Page 6

You might also like