In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as aniteration statement. The keyword used to declare a for loop is based on the heritage of the language and the prior programming languages it borrowed from, so programming languages that are descendants of or offshoots of a language that originally developed an iterator will often use the same keyword to name an iterator, e.g. descendants of ALGOL will use "for", while descendants of Fortran will use "do." Some languages started from scratch, and didn't have a predefined term for an iterator to choose from, which is why COBOL uses "PERFORM VARYING".
Unlike many other kinds of loops, such as the while loop, the for loop is often distinguished by an explicit loop counter or loop variable. This allows the body of the for loop (the code that is being repeatedly executed) to know about the sequencing of each iteration. For loops are also typically used when the amount of iterations is known before entering the loop. For loops are the shorthand way to make loops when the number of iterations is known, as a for loop can be written as a while loop.
The name for loop comes from the English word for, which is used as the keyword in most programming languages to introduce a for loop. The loop body is executed "for" the given values of the loop variable, though this is more explicit in the ALGOL version of the statement, in which a list of possible values and/or increments can be specified.
No comments:
Post a Comment