In: Computer Science
Provide explanation about Iterations. Give examples and write a program.
Iterations are simply said to be executing some
set of statements continuously for 'n' number of times or the
condition is encountered.
Cycle of instructions are performed in a repeated manner called
'loops'.
There are two types of iteration:
1)Count controlled iterations: This iteration is used for repeat the statements or steps a specific number of times. In this iteration you already know the how many iterations it takes.
for example, FOR loop. For loop is comes under this count
controlled iterations.
example in python language:
n=5
for i in range(0,n):
print(i)
output: 0,1,2,3,4
2)Condition controlled iterations: This
iteration is used for repeating the statements continuously until
the condition is satisfied. In this case the number of iterations
are unknown. It follows the condition is true or false. If true,
statement is executed otherwise not executed.
for example, While loops comes under this condition controlled
iterations.
examples in python language:
n=6
a=1
while(a<n):
print(a)
a=a+1
output: 1 2 3 4 5