SUPPORT THE WORK

GetWiki

For loop

ARTICLE SUBJECTS
aesthetics  →
being  →
complexity  →
database  →
enterprise  →
ethics  →
fiction  →
history  →
internet  →
knowledge  →
language  →
licensing  →
linux  →
logic  →
method  →
news  →
perception  →
philosophy  →
policy  →
purpose  →
religion  →
science  →
sociology  →
software  →
truth  →
unix  →
wiki  →
ARTICLE TYPES
essay  →
feed  →
help  →
system  →
wiki  →
ARTICLE ORIGINS
critical  →
discussion  →
forked  →
imported  →
original  →
For loop
[ temporary import ]
please note:
- the content below is remote from Wikipedia
- it has been imported raw for GetWiki
{{Short description|Control flow statement for repeated execution}}File:For loop example.svg|thumb|right|Flow diagram of the following for loop code:for (i = 0; i < 5; i++)
printf(“*“);
The loop will cause five asterisks to be printed.{{Loop constructs}}In computer science a for-loop or for loop is a control flow 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 that is executed once per iteration. The header often declares an explicit loop counter or loop 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-loops 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.BOOK, Wirth, Niklaus, Niklaus Wirth, 1973, Preface, Systematic Programming: An Introduction, xiii, 0138803692, 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.

FOR

(File:For loop.svg|thumb|350px|For loop illustration, from i=0 to i=2, resulting in data1=200)A for-loop statement is available in most imperative programming languages. Even ignoring minor differences in syntax, there are many differences in how these statements work and the level of expressiveness they support. Generally, for-loops fall into one of four categories:

Traditional for-loops

The for-loop of languages like ALGOL, Simula, BASIC, Pascal, Modula, Oberon, Ada, MATLAB, OCaml, F#, and so on, requires a control variable with start- and end-values, which looks something like this:for i = first to last do statement(* or just *)for i = first..last do statementDepending on the language, an explicit 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 differs 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 language. It requires 3 parts: the initialization (loop variant), the condition, and the advancement to the next iteration. All these three parts are optional.WEB,www.learncpp.com/cpp-tutorial/57-for-statements/, For loops in C++, Learn C++, This type of “semicolon loops” came from B programming language and it was originally invented by Stephen Johnson.AV MEDIA, Thompson, Ken, Ken Thompson, YouTube,www.youtube.com/watch?v=EY6q5dv_B-o&t=2330, VCF East 2019 – Brian Kernighan interviews Ken Thompson,ghostarchive.org/varchive/youtube/20211212/EY6q5dv_B-o, 2021-12-12, live, I saw Johnson’s semicolon version of the for loop and I put that in [B], I stole it., 2020-11-16, {{cbignore}}In the initialization part, any variables needed are declared (and usually assigned values). If multiple variables are declared, they should all be of 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.// Prints the numbers from 0 to 99 (and not 100), each followed by a space.for (int i=0; i 1000.0) exit
sumsq = sumsq + i**2
end do
print *, sumsq


end program

1958: ALGOL

{{Further|ALGOL 58}}ALGOL 58 introduced the {{code|for}} statement, using the form as Superplan:
FOR Identifier = Base (Difference) Limit
For example to print 0 to 10 incremented by 1:FOR x = 0 (1) 10 BEGINPRINT (FL) = x END

1960: COBOL

{{Further|COBOL}}COBOL was formalized in late 1959 and has had many elaborations. It uses the PERFORM verb which has many options. Originally all loops had to be out-of-line with the iterated code occupying a separate paragraph. Ignoring the need for declaring and initialising variables, the COBOL equivalent of a for-loop would be.
PERFORM SQ-ROUTINE VARYING I FROM 1 BY 1 UNTIL I > 1000


SQ-ROUTINE
ADD I**2 TO SUM-SQ.
In the 1980s, the addition of in-line loops and structured programming statements such as END-PERFORM resulted in a for-loop with a more familiar structure.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 1000
ADD I**2 TO SUM-SQ.
END-PERFORM
If the PERFORM verb has the optional clause TEST AFTER, the resulting loop is slightly different: the loop body is executed at least once, before any test.

1964: BASIC

{{Further|BASIC}}In BASIC, a loop is sometimes named a for-next loop.10 REM THIS FOR LOOP PRINTS ODD NUMBERS FROM 1 TO 1520 FOR I = 1 TO 15 STEP 230 PRINT I40 NEXT IThe end-loop marker specifies the name of the index variable, which must correspond to the name of the index variable in the start of the for-loop. Some languages (PL/I, Fortran 95 and later) allow a statement label on the start of a for-loop that can be matched by the compiler against the same text on the corresponding end-loop statement. Fortran also allows the {{code|EXIT}} and {{code|CYCLE}} statements to name this text; in a nest of loops this makes clear which loop is intended. However, in these languages the labels must be unique, so successive loops involving the same index variable cannot use the same text nor can a label be the same as the name of a variable, such as the index variable for the loop.

1964: PL/I

{{Further|PL/I}}do counter = 1 to 5 by 1; /* “by 1” is the default if not specified */
/*statements*/;
end;
The {{mono|LEAVE}} statement may be used to exit the loop. Loops can be labeled, and leave may leave a specific labeled loop in a group of nested loops. Some PL/I dialects include the {{mono|ITERATE}} statement to terminate the current loop iteration and begin the next.

1968: ALGOL 68

{{Further|ALGOL 68}}ALGOL 68 has what was considered the universal loop, the full syntax is:FOR i FROM 1 BY 2 TO 3 WHILE i≠4 DO ~ ODFurther, the single iteration range could be replaced by a list of such ranges. There are several unusual aspects of the construct
  • only the {{code|do ~ od}} portion was compulsory, in which case the loop will iterate indefinitely.
  • thus the clause {{code|to 100 do ~ od}}, will iterate exactly 100 times.
  • the {{code|while}} syntactic element allowed a programmer to break from a {{code|for}} loop early, as in:
INT sum sq := 0;FOR i
WHILE
print((“So far:”, i, new line)); # Interposed for tracing purposes. #
sum sq ≠ 70↑2 # This is the test for the WHILE #
DO
sum sq +:= i↑2
ODSubsequent extensions to the standard ALGOL 68 allowed the {{code|to}} syntactic element to be replaced with {{code|{{sic|hide=y|up|to}}}} and {{code|downto}} to achieve a small optimization. The same compilers also incorporated:
{{code|until}}: for late loop termination.
{{code|foreach}}: for working on arrays in parallel.

1970: Pascal

{{Further|Pascal (programming language)}}for Counter := 1 to 5 do
(*statement*);
Decrementing (counting backwards) is using {{code|downto}} keyword instead of {{code|to}}, as in:for Counter := 5 downto 1 do
(*statement*);
The numeric-range for-loop varies somewhat more.

1972: C, C++

{{Further|C (programming language)|C++|C syntax#Iteration statements}}for (initialization; condition; increment/decrement)
statement
The {{mono|statement}} is often a block statement; an example of this would be://Using for-loops to add numbers 1 - 5int sum = 0;for (int i = 1; i

- content above as imported from Wikipedia
- "For loop" does not exist on GetWiki (yet)
- time: 4:25am EDT - Wed, May 22 2024
[ this remote article is provided by Wikipedia ]
LATEST EDITS [ see all ]
GETWIKI 21 MAY 2024
GETWIKI 09 JUL 2019
Eastern Philosophy
History of Philosophy
GETWIKI 09 MAY 2016
GETWIKI 18 OCT 2015
M.R.M. Parrott
Biographies
GETWIKI 20 AUG 2014
CONNECT