What is a count-controlled loop?
In Python, a count-controlled loop is a type of loop structure that iterates a specific number of times based on a counter variable.
for'
loop is commonly used for count-controlled loops. It allows you to iterate over a sequence of elements or execute a block of code a fixed number of times.For example code:
for i in range(1, 6): # Loop from 1 to 5 (inclusive)
print("Iteration:", i)
Output of the code:
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Explanation of the code:
range(1, 6)
function generates a sequence of numbers from 1 to 5. The loop variable i
takes the value of each number in the sequence, and the print statement displays the current iteration number.