Jump to content

Talk:For loop

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 76.175.72.51 (talk) at 05:57, 4 December 2008 (→‎Distinguishing iterator-based and numeric for loops). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Factorials?

hmm is the factorial example the best and most easy to understand example? because i don't really thing it is. Matthew 12:14, 21 January 2006 (UTC)[reply]

I would tend to agree. We should use something that isn't math-specific. --Hooperbloob 19:11, 21 January 2006 (UTC)[reply]
I've ditched it in favor of no actual content in the example. Stuff like factorials, declaring variables, and printing were distracting from the actual for loop. —Simetrical (talk • contribs) 04:40, 25 August 2006 (UTC)[reply]
This might be true - but Do while loop and While loop still have the factorial examples and in a larger picture is was quite nice to see the same algorithm with different loop constructs and different languages. —Preceding unsigned comment added by Krischik (talkcontribs) 14:44, 1 February 2008 (UTC)[reply]

bash

From the bash section (before I changed it):

#subroutine for listing consecutive integers
function range ()
{
	if [ $1 -ge $2 ]; 
	then
		return
	fi
	i=$1
	while [ $i -le $2 ]; do
		echo $i;
		i=$(($i+1));
	done
}

#main
factorial=1
for counter in $(range 1 5)
do
	factorial=$(($factorial * $counter))
done
echo $factorial

Note that bash does not have the built-in ability to list consecutive integers…

Is this really correct? The following code works perfect in my bash 3.00:

factorial=1
for ((i=1; $i<=5; i=$i+1)) do 
    factorial=$(($factorial * $i)) 
done 
echo $factorial

Am I missing something? –Gustavb 02:59, 11 February 2006 (UTC)[reply]

Merge Foreach into this article

I propose that the Foreach article should be merged into this one. That other article significantly overlaps this one, now that more detail has been created here which describes the variety of for loops. Although it may use the keyword foreach rather than for, the semantics of the loops are the same, and in fact the foreach loop is really just a specific kind of for loop. — Dmeranda 17:02, 17 May 2006 (UTC)[reply]

I disagree. The examples of for loops discussed in the for loop article are exclusively about traversing a numeric range with a loop counter, whether using the "numeric ranges" or the "three-expression for loops". The foreach loop is semantically very different, and is characterized by the lack of a loop counter (and sometimes lack of guarantee on order of traversal), and is specifically used to operate on each item of a collection. In any language that supports both the for loop and the foreach loop, they are treated as very different constructs, syntatically and semantically. I think that the "for loop" article should not include the "foreach loop" as a type of for loop, even though they sound the same; and instead link to it as a different type of thing. --Spoon! 23:26, 17 August 2006 (UTC)[reply]

They are two similar but distinct concepts, it would confuse anyone who didn't understand the differences. — Preceding unsigned comment added by 82.41.27.90 (talkcontribs) 20:23, 15 July 2006 (UTC)[reply]

Keep For and Foreach separate. They are not the same things. You can reference this article from for and the for article from this one but don't merge them. — Preceding unsigned comment added by 69.95.130.62 (talkcontribs) 20:21, 20 July 2006 (UTC)[reply]

The thing is, Python and many other languages use the keyword for for what PHP calls foreach. We can't say that that's not a "real" for loop; it has to be discussed here. I've linked to it as the main article for the iterator loop: is that good with everyone? —Simetrical (talk • contribs) 04:16, 25 August 2006 (UTC)[reply]
What languages other than PHP actually use the term foreach anyway? —Simetrical (talk • contribs) 04:41, 25 August 2006 (UTC)[reply]
Apparently the last edit to the Foreach article deleted all the code samples. Looking over what was there, C#, Perl (although confusingly "for" can also be used), PHP, Realbasic (two words "for each"), Tcl, and maybe others --Spoon! 01:53, 2 September 2006 (UTC)[reply]
Correct. The goal of the Foreach article shouldn't be to tell you how every last language writes its foreach loops, it should just give an overview of the general syntaxes used. I may have been slightly overzealous, though; I've added a sentence to the "Syntaxes" section to expand a bit on the differences. —Simetrical (talk • contribs) 21:29, 3 September 2006 (UTC)[reply]

Fortran has both 'DO' and 'FOR' loops

Need to update article. Rwwww 07:33, 31 July 2006 (UTC)[reply]

Can you provide a reference for this assertion? According to the ISO/IEC DIS 1539-1:2004(E), 2004-May-3 [1] (e.g., FORTRAN 2003) there is no FOR statement, only a DO and a DO WHILE. There is though a FOREACH construct, but it is not a loop control statement.--Dmeranda 20:47, 31 July 2006 (UTC) —Edit: Sorry, meant FORALL, not FOREACH.--Dmeranda 20:53, 31 July 2006 (UTC)[reply]

When did this name appear?

Does anybody know when the first use of the word "for loop" for this concept occured? Heinz Rutishauser had a so called "Für"-loop in his language "Superplan" (designed from 1949 to 1951) which has the same semantics as the for loop in ALGOL 60 (to which Rustishauser contributed). It would be interesting to find out where and when exactly the name originated.

Für i=2(1)n :    + 3 =   

means, that in an array a all elements i (steps of 1) from 2 to n are incremented by 3.
(cf. discussion of Plankalkül)
217.236.218.47 19:38, 26 June 2007 (UTC)[reply]

It may be difficult to determine the exact origin because those are also ordinary English words and their meaning in "for loop" is really not very far from their non-technical use. There may also be a distinction between a language having a construct that is similar to the for loop or that uses a keyword "for", versus casual discussion of it using the term "for loop". That being said, it is possible that it derives from the mathematical universal quantifier (), which is typically read aloud as "for all". Among early languages ALGOL 58 certainly had a for loop although the syntax would be considered unusual today. Another early language, BCPL, from 1966 also had a for loop, and a being a predecessor to C looked more like the modern for loop and may have been more widely discussed (thus leading to the use of the term "for loop", although this would be conjecture). Note though that Fortran, one of the most popular 1950s language had a "DO" loop, which aside from using "do" rather than "for" is semantically quite similar to the modern for loop. - Dmeranda 13:21, 28 June 2007 (UTC)[reply]
Right, tracing back the semantics should be difficult, and consequently also the "casual discussion use" of "for loops". But is there any earlier explicit use of "for", German "Für" as a keyword for this concept? Rutishauser was a member of both meetings, the one that developed ALGOL 58, as well as the one that conceived ALGOL 60, and the syntax of his for loop looks pretty much like the ALGOL one - even considering ALGOL 68 it only misses the condition. A probable guess is that this part was contributed to ALGOL by Rutishauser - and "für" simply translated to "for" as a keyword suggestion.
217.236.210.65 09:52, 30 June 2007 (UTC)[reply]
PS Concerning the origin of the use of for/für: having designed Superplan from 1949 to 1951, the only other higher level language was Plankalkül, anyway - no FORTRAN etc. around at that time.
If you can cite a verifiable reference to the Superplan language and this loop construct you are talking about then please add it, especially if there is a documented direct or indirect lineage to ALGOL. - Dmeranda 07:08, 2 July 2007 (UTC)[reply]


Hello
The following is a German book by Dr. Hans Willy-Hohn
[2],
And on page 185 he writes the following:
Here is a translation (the original follows):
"Thus through the involvement of the Institute for Practical Mathematics in Zürich Zuse's Plankalkül was by chance again part of their considerations – Bauer and Samelson had beforehand ignored it. The Institute for Practical Mathematics had bought Zuse's Z4 in 1949 (...). H. Rutishauser, one of the members of the institute, who was in charge of programming the machine, got to know Zuse's Plankalkül-writings. Rutishauser published an essay on Automatische Rechenplanfertigung [meaning: automatical generation of machine code] in the Magazine for Applied Mathematics in 1951, in which he showed, that die programmgesteuerte Rechenmaschine selbst dank ihrer Vielseitigkeit als Planfertigungsgerät [meaning: a programmed computing machine can – thanks to its diversity - itself be used as a means to produce machine code]. There he described for a simple algebraic programming language which referring to Zuse's terminology he called Superplan, one of the first compilers (...). For Bauer and Samelson this essay was the reason to ask Rutishauser to join in the development of ALGOL. Hereby parts of Plankalkül influenced ALGOL (Bauer 1980)."
original:
"Durch einen Zufall kam als folge der Beteiligung des Instituts für praktische Mathematik in Zürich auch der Plankalkül von Konrad Zuse wieder ins Spiel, nachdem auch Bauer und Samelson dieser Programmiersprache zunächst keine Beachtung geschenkt hatten. Das Institut für praktische Mathematik hatte 1949 Zuses Z4 erworben (...). H. Rutishauser, einer der Mitarbeiter des Instituts, der für die Programmierung der Maschine zuständig war, hatte dabei auch Zuses Arbeiten über den Plankalkül kennengelernt. In der Zeitschrift für angewandte Mathematik veröffentlichte Rutishauser 1951 einen Aufsatz über Automatische Rechenplanfertigung, in dem er zeigte, daß sich die programmgesteuerte Rechenmaschine selbst dank ihrer Vielseitigkeit als Planfertigungsgerät (...) verwenden ließ. Er beschrieb dort für eine einfache algebraische Programmiersprache, die er in Anlehnung an Zuses Terminologie als Superplan bezeichnete, einen der weltweit ersten Compiler (...). ... Bauer und Samelson wiederum nahmen dann diesen Aufsatz zum Anlaß, Rutishauser an der Entwicklung von ALGOL zu beteiligen. Hierdurch wiederum sind Teile des Plankalküls in ALGOL eingeflossen (Bauer 1980)."
In this paper on the development of ALGOL you can see an example of the syntax of Rutishauser's language.
[3] HT de Beer, The History of the ALGOL Effort (PDF)
Superplan is mentioned here, too.
Sincerely, 217.236.255.112 09:50, 2 July 2007 (UTC)[reply]
This looks like good research material related to this article, unfortunately I don't read German (thanks for the translation). Why don't you take the opportunity and jump in and add some of this information to the article itself. I'd suggest creating a Wikipedia account first, and if you need it there's a lot of help on editing Wikipedia. There's also specific guidance on how to cite sources. - Dmeranda
Actually I have an account – I just don't always bother to sign in.
Maybe somebody else has further info on the first appearance of the keyword "for" or explicit and forthright evidence that Rutishauser was the source for this keyword and syntax used in ALGOL.
I think the best way to proceed would be to create sourced articles about Superplan and Rutishauser, anyway. But I don't have further information about Superplan.
Christian Storm 18:41, 3 July 2007 (UTC)[reply]
I don't know much on Superplan. If you don't get any help here I'd suggest going over to the talk pages for ALGOL, it probably has a much larger audience than this article. Or better yet just go ahead and start a stub article for Superplan with the material you seem to have; it's already linked from Timeline of programming languages. Since it looks like your cited materials are of German origin, you may also find some information or helpers on the German Wikipedia site. And you may want to see what's up in the Wikipedia:WikiProject Programming languages too. -- Dmeranda 21:16, 3 July 2007 (UTC)[reply]

Distinguishing iterator-based and numeric for loops

I've marked the section Distinguishing iterator-based and numeric for loops with a no original research tag. Please note that I am not saying that the information presented is incorrect or not worthy or inclusion. However, as this section is highly technical and is presented in a casual prescriptive style, it would be useful if some citations were made in this case. Additionally, I'm not entirely sure the subject belongs in this article, as it really seems to be more about loop optimization or vectorization than it is about the for loop in particular. Consider perhaps moving or merging the bulk of this section to another article as well as trying to cite some references. Welcome to discuss further if other opinions exist. - Dmeranda 21:32, 2 July 2007 (UTC)[reply]

As the guilty party, I wonder at the "no original research" marker - is this an assertion that x is original research, or a prescription that there should not be such, somewhere? Anyway, I recall last year coding a LU-decomposition using both for and forall and experimenting to establish that indeed the compiler does generate code that behaves as described in the manual for the compaq fortran 95 compiler, and indeed that ignoring the detail I described leads to wrong results. This is hardly original research, simply a direct report of experience as warning to others who might not notice yet another vaguely general statement in some manual with (as so often) no example to illustrate the consequences. (My copy has handwritten annotations, though I'm the only user) References to a specific compiler manual seem otiose.
As for the style, I can't be bothered with a prim pseudo-generality that obscures simple details (nor am I being paid for it): the text is of course subject to the whims of all. The best location is in doubt for me also. Loop optimisation and array manipulation is a large subject with many details, and indeed there could be links to that. But an article that discusses both for and forall loops should be able to distinguish them. NickyMcLean 22:30, 2 July 2007 (UTC)[reply]
This isn't the place for your criticisms of the design of a language. 76.175.72.51 (talk) 05:54, 4 December 2008 (UTC)[reply]