For loop

In computer programming, a for loop is a structured control flow statement that repeatedly runs a section of code until a condition is satisfied.

A for loop has two parts: a header and a body. The header defines how the loop iterates, and the body is the code executed once per iteration. The header often declares a loop variable which can be used in the body to know which iteration of the loop is being executed. A relatively simple for loop iterates for a fixed number of times. For example, the following C for loop declares a loop variable i and prints its value as it increments from 0, 1, and 2:

for (int i = 0; i < 3; ++i) {
    printf("%d", i);
}

Depending on programming language, various keywords are used to denote a for loop. For example, descendants of ALGOL use for,[1] while descendants of Fortran use do and COBOL uses PERFORM VARYING.

  1. ^ Wirth, Niklaus (1973). "Preface". Systematic Programming: An Introduction. Prentice-Hall. pp. xiii. ISBN 0138803692.