Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Loop Control Statements

Uploaded by

rudosereberuc.gk
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Loop Control Statements

Uploaded by

rudosereberuc.gk
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

LOOP CONTROL

STATEMENTS
WHAT IS A LOOP?

The process of repetitive execution of a


statement or a sequence of statements is called a loop.
Using loops, we need to write only once the sequence of
statements to be repeatedly executed. Execution of a
sequence of statements in a loop is known as an iteration
of the loop.
for loop : The control statement for is used when we want to
execute a sequence of statements a fixed number of times
while loop : The while loop is used for executing a sequence of
statements again and again on the basis of some test
condition. If the test condition holds True, the body of
the loop is executed, otherwise the control moves to the
statement immediately following the while loop.
Nested loops : A loop inside another loop. Nesting may continue to many levels

• Break : to exit a loop


• Continue: to transfer the control to next iteration of the loop
USE OF FOR LOOP
TO FIND SUM OF FIRST N NATURAL NUMBERS
Program:
n = int(input("Enter a positive integer: "))
sum=0
for i in range(1, n + 1):
sum+=i
print(f"The sum of the first",n," natural numbers is:",sum)
Output:
Enter a positive integer: 6
The sum of the first 6 natural numbers is: 21
TO FIND PERCENTAGE
n = int(input("Enter the number of subjects: "))

total_marks = 0
for i in range(1, n + 1):
marks = float(input("Enter marks for subject : "))
total_marks += marks

maximummarks = float(input("Enter the maximum marks for each subject: "))

total_max_marks = n * maximummarks
percentage = (total_marks / total_max_marks) * 100

print("Total marks obtained:",total_marks)


print("Percentage:",percentage)

Output
Enter the number of subjects: 2
Enter marks for subject : 40
Enter marks for subject : 45
Enter the maximum marks for each subject: 50
Total marks obtained: 85.0
Percentage: 85.0
USE OF WHILE LOOP
TO FIND SUM OF DIGITS OF A
NUMBER
TO FIND FACTORIAL OF A NUMBER
TO FIND REVERSE OF A NUMBER
TO FIND ARMSTRONG NUMBER
NESTED LOOPS
PROGRAM TO DEMONSTRATE THE
USE OF THE NESTED FOR LOOP
for i in range(1,4,1):
for j in range(1,4,1):
print("i=",i,"j=",j,"i+j=",i+j)
print("End of program")
Output:
i= 1 j= 1 i+j= 2
i= 1 j= 2 i+j= 3
i= 1 j= 3 i+j= 4
i= 2 j= 1 i+j= 3
i= 2 j= 2 i+j= 4
i= 2 j= 3 i+j= 5
i= 3 j= 1 i+j= 4
i= 3 j= 2 i+j= 5
i= 3 j= 3 i+j= 6
End of program
MULTIPLICATION TABLE USIN
print(“Multiplication Table from 1 to 5 “)
for i in range(1,11,1):
for j in range(1,6,1):
print(format(i * j,”4d”),end=” ”)
print()
print(”end of program”)
Output:
Multiplication Table from 1 to 5
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
6 12 18 24 30
7 14 21 28 35
8 16 24 32 40
9 18 27 36 45
10 20 30 40 50
end of program
STAR PATTERN
print(" Star Pattern Display")
num=7
x=num
for i in range(1,6,1):
num=num-1;
for j in range(1,num,1):
print("*",end="")
x=num-1
print()
print("End of program")

Output:

Star Pattern Display


*****
****
***
**
*
End of program
NUMBER PATTERN
print(" Number Pattern Display")
num=1
x=num
for i in range(1,6,1):
num=num+1;
for j in range(1,num,1):
print(j, end="")
x=num+1
print()
print("end of program")
Output:
1
12
123
1234
12345
end of program
THE break
STATEMENT
AN EXAMPLE
print(“The Numbers from 1 to 10 are as follows:”)
for i in range(1,100,1):
if(i==11):
break
else:
print(i, end=“”)
Output:
The Numbers from 1 to 10 are as follows:
1 2 3 4 5 6 7 8 9 10
THE continue
STATEMENT
EXAMPLE
for i in range(1,11,1):
if i == 5:
continue
print(i, end=” “)
Output:
1 2 3 4 6 7 8 9 10

You might also like