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.