For loop facts for kids
In computer science, a for-loop (or “for loop”) is used to run specified lines of code repeatedly. In different languages, there are different keywords to specify this statement—ways to identify a for loop—but they all do the same task. Most languages use either “for” or “do” as keywords.
A for-loop has two parts: a header, and a body of code. The body consists of a set of instructions (lines of code) that run for each repetition of the loop. The header often declares how many loops to compete, generally indicated by either a loop counter or a loop variable. For-loops are typically used when the number of repetitions are known. For-loops are similar to “while-loops”, but usually have a known number of repetitions.
Sample for-loop in Python:
for i in range(10):
print('Hello, world!')
This for loop prints outs "Hello, world!" ten times.
The name for-loop comes from the English word for, which is the direct translation of the earlier German für, used by Heinz Rutishauser, who also helped define ALGOL 58 and 60.
Contents
Loop Counter
In computer science, a loop counter is a variable that controls how many repetitions the loop will do. Usually, the loop counter variable is an integer value that increments by 1 for each completed loop. However, loop counters can also decrement, and can have step sizes other than 1.
Loop counters change each iteration. With each repeat, there is unique counter value. The loop counter is the deciding factor for when a loop should end and go onto the next section of code.
Common identifier names for loop counters are i, j, and k(and so on, if needed).
for loop
A for-loop is available in most common programming languages. While for-loops in different languages will all fulfill the same task, there are differences on how each programming language deals with for loops.
Images for kids
See also
In Spanish: Bucle for para niños