Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
For loop
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Control flow statement for repeated execution}} [[File:For loop example.svg|thumb|right|Flow diagram of the following for loop code: <syntaxhighlight lang="c"> for (i = 0; i < 5; i++) printf("*"); </syntaxhighlight> The loop will cause five asterisks to be printed.]] {{Loop constructs}}<!-- DO NOT remove. Discuss navigation concept at [[Talk:Do while loop#Helpbox experiment]] --> In [[computer science]], a '''for-loop''' or '''for loop''' is a [[control flow]] [[Statement (computer science)|statement]] for specifying [[iteration]]. Specifically, a for-loop functions by running a section of code repeatedly until a certain condition has been satisfied. For-loops have two parts: a header and a body. The header defines the iteration and the body is the code executed once per iteration. The header often declares an explicit [[For loop#Loop counters|loop counter]] or loop [[Variable (computer science)|variable]]. This allows the body to know which iteration is being executed. For-loops are typically used when the number of iterations is known before entering the loop. For-loops can be thought of as shorthands for [[while-loop]]s which increment and test a loop variable. Various keywords are used to indicate the usage of a for loop: descendants of [[ALGOL]] use "{{mono|for}}", while descendants of [[Fortran]] use "{{mono|do}}". There are other possibilities, for example [[COBOL]] which uses {{nowrap|{{code|PERFORM VARYING}}}}. The name ''for-loop'' comes from the word [[Wiktionary: for|for]]. ''For'' is used as the [[reserved word]] (or keyword) in many programming languages to introduce a for-loop. The term in English dates to [[ALGOL 58]] and was popularized in [[ALGOL 60]]. It is the direct translation of the earlier German {{Wikt-lang|de|fΓΌr}} and was used in [[Superplan]] (1949β1951) by [[Heinz Rutishauser]]. Rutishauser was involved in defining ALGOL 58 and ALGOL 60.<ref>{{Cite book |last=Wirth |first=Niklaus |author-link=Niklaus Wirth |year=1973 |chapter=Preface |title=Systematic Programming: An Introduction |pages=xiii |publisher=Prentice-Hall |isbn=0138803692}}</ref> The loop body is executed "for" the given values of the loop variable. This is more explicit in [[ALGOL]] versions of the for statement where a list of possible values and increments can be specified. In Fortran and [[PL/I]], the keyword {{mono|DO}} is used for the same thing and it is named a ''do-loop''; this is different from a ''[[do while loop]]''.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)