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!
===Traditional for-loops=== The for-loop of languages like [[ALGOL]], [[Simula]], [[BASIC]], [[Pascal (programming language)|Pascal]], [[Modula]], [[Oberon (programming language)|Oberon]], [[Ada (programming language)|Ada]], [[MATLAB]], [[OCaml]], [[F Sharp (programming language)|F#]], and so on, requires a [[Control flow|control variable]] with start- and end-values, which looks something like this: <syntaxhighlight Lang="pascal"> for i = first to last do statement (* or just *) for i = first..last do statement </syntaxhighlight> Depending on the language, an explicit [[Assignment (computer science)|assignment]] sign may be used in place of the [[equal sign]] (and some languages require the word {{code|int}} even in the numerical case). An optional step-value (an increment or decrement β 1) may also be included, although the exact syntaxes used for this differ a bit more between the languages. Some languages require a separate declaration of the control variable, some do not. Another form was popularized by the [[C (programming language)|C language]]. It requires 3 parts: the [[Declaration (computer programming)|initialization]] ([[loop variant]]), the [[Boolean algebra|condition]], and the advancement to the next iteration. All these three parts are optional. This type of "semicolon loops" came from [[B (programming language)|B programming language]] and it was originally invented by [[Stephen C. Johnson|Stephen Johnson]].<ref name="ken">{{cite AV media |last=Thompson |first=Ken |author-link=Ken Thompson |website=[[YouTube]] |url=https://www.youtube.com/watch?v=EY6q5dv_B-o&t=2330 |title=VCF East 2019 β Brian Kernighan interviews Ken Thompson |archive-url=https://ghostarchive.org/varchive/youtube/20211212/EY6q5dv_B-o |archive-date=2021-12-12 |url-status=live |quote=I saw Johnson's semicolon version of the for loop and I put that in [B], I stole it. |access-date=2020-11-16}}{{cbignore}}</ref> In the initialization part, any variables needed are declared (and usually assigned values). If multiple variables are declared, they should all be the same type. The condition part checks a certain condition and exits the loop if false, even if the loop is never executed. If the condition is true, then the lines of code inside the loop are executed. The advancement to the next iteration part is performed exactly once every time the loop ends. The loop is then repeated if the condition evaluates to true. Here is an example of the C-style traditional for-loop in [[Java (programming language)|Java]]. <syntaxhighlight Lang="java"> // Prints the numbers from 0 to 99 (and not 100), each followed by a space. for (int i=0; i<100; i++) { System.out.print(i); System.out.print(' '); } System.out.println(); </syntaxhighlight> These loops are also sometimes named ''numeric for-loops'' when contrasted with foreach loops (see below).
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)