For loop

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

In computer science, a for-loop (or “for loop”) is used to execute a block of code repeatedly. In different languages, there are different keywords to specify this statement, 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 list of possible values and/or increments can be specified and is completed 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.

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.

Loop Counter[change | change source]

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[change | change source]

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.