a loop is method or a way provided by programming languages to perform a repeated task or iterate on a number :
For loops are usually used , when we know the end limits like when we know how many times certain task is needed to be performed !!
example:
Q. suppose you want to print a name 10 time
for i in range(0,10): #loop will go from 0 to 9 i.e 10 times
print("a name") # it will print " a name" 10 times
or
print("a name", end= " ") # to get output in horizontal line like : a name a name a name ................ a name
output:
a name
a name
.
.
.
.
.
a name
Q. now suppose you want to print numbers which are even from 0 to 100 .
for i in range(0, 100): # i is going from 0 to 100
if (i % 2 == 0): # each value of i is checked by condition
print( i , end = " ") # printing every value of i in horizontal manner
output:
0 2 4 6 8 10 12 . . ......................... ... .. . . . . . . . . .. . . . . . . . . . . . 98
Thankyou
For loops are usually used , when we know the end limits like when we know how many times certain task is needed to be performed !!
example:
Q. suppose you want to print a name 10 time
for i in range(0,10): #loop will go from 0 to 9 i.e 10 times
print("a name") # it will print " a name" 10 times
or
print("a name", end= " ") # to get output in horizontal line like : a name a name a name ................ a name
output:
a name
a name
.
.
.
.
.
a name
Q. now suppose you want to print numbers which are even from 0 to 100 .
for i in range(0, 100): # i is going from 0 to 100
if (i % 2 == 0): # each value of i is checked by condition
print( i , end = " ") # printing every value of i in horizontal manner
output:
0 2 4 6 8 10 12 . . ......................... ... .. . . . . . . . . .. . . . . . . . . . . . 98
Thankyou
Comments
Post a Comment