Talk:For loop

message: ...seems to be entirely absent from this article. It's well-known, but for a beginner, it's not immediately obvious what they each do. They have generally accepted names, correct? Would someone who knows those names add them? 24.58.56.142 (talk) 22:50, 17 May 2012 (UTC)
replies: text: Requested quite a while ago. I attempted to fix this, along with a citation from a C++ website. -- Carrot Lord (talk) 17:17, 21 December 2012 (UTC)
thread_title:  Explanation of the three-part loop syntax 

message: hmm is the factorial example the best and most easy to understand example? because i don't really think it is.
Matthew 12:14, 21 January 2006 (UTC)
replies: text: I would tend to agree. We should use something that isn't math-specific. --Hooperbloob 19:11, 21 January 2006 (UTC)

text: 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)

text: 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 (talk • contribs) 14:44, 1 February 2008 (UTC)
thread_title: Factorials?

message: 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)
thread_title:  bash 

message: 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)
replies: text: 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)

text: 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)
replies: text: What languages other than PHP actually use the term foreach anyway? —Simetrical (talk • contribs) 04:41, 25 August 2006 (UTC)
replies: text: 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)
replies: text: 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)
thread_title:  Merge [[Foreach]] into this article 

message: Need to update article. Rwwww 07:33, 31 July 2006 (UTC)
replies: text: Can you provide a reference for this assertion?  According to the ISO/IEC DIS 1539-1:2004(E), 2004-May-3  (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)
thread_title: Fortran has both 'DO' and 'FOR' loops

message: The diagram shows that for(A;B;C) D; does NOT execute C in the last loop iteration, however the text states:
"Finally, the counting expression is evaluated at the end of each loop iteration."
This gives conflicting results for the following code:
int i;
for(i=0;i<10;i++); // do nothing
Now i may be either 10 (according to the diagram) or 11 (according to the text). 
A quick test with java told me 10 is the correct value so at least for java the diagram is correct and the text is wrong. As I'm not used to editing wikipedia I just put it in the discussion however should there may be some mistake on my part :-)  — Preceding unsigned comment added by 139.18.8.113 (talk) 13:51, 28 June 2011 (UTC)
replies: text: Seems to have been corrected in the meantime - the diagram shows correctly that C will be executed after the last D, then condition B will return false, thus exiting the loop. And how should i get 11? The condition is already false when i is 10 (10<10 is false). Bfroehler (talk) 08:22, 27 September 2011 (UTC)
thread_title:  Contradiction between text and diagram 

message: The Algol example, before or after the recent re-prettying is a puzzle to me. Does the Algol syntax really allow a print statement before the logical expression of a WHILE clause? And although it may be fashionable now to put various clauses on a line of their own, it took me a while to realise that the vertical straggle was indeed one FOR ... WHILE ... DO construction and thus that the "print" was interposed. It is useful to have the various examples compute the same thing in their different ways, and show the same exit-from-loop if the sum of squares exceeds 1000, but, the Algol example appears to use the test Sum Sq not equal to seventy squared - which it will be for i = 24, but Sum Sq passes 1000 with i = 14. And does Algol allow spaces in names - I thought only Fortran was space-ignoring? And, I don't see any increment of the index variable in the Algol example either! Is this some default? I could rejigger the example as I have used Algol, but I did not use an Algol with the preceeding abilities, or so I thought at the time. Confusion! NickyMcLean (talk) 21:09, 10 July 2011 (UTC)
replies: text: The "reprettying" has lost the structure of the statement.  In Algol, the compound statement  a; b  executes a and b in sequence, and has the value of b, and while a; b do  means the same as while (a; b) do . --Macrakis (talk) 19:08, 11 July 2011 (UTC)
thread_title: Algol Example a puzzle

message: Does anybody have a reference to an official document (like the ISO standard) to where it says that in C++, for loop variables are immutable in the loop body (see section "Loop variable scope and semantics", second abstract)? The compiler I use (Microsoft Visual C++ 2010) does NOT behave like it was immutable - no warning from the compiler when I modify the variable, and the behavior of the program is also as I would expect; example:
 #include <iostream>
 int main(int argc, char** argv)
 {
 	for (int i=0; i<3; ++i)
 	{
 		if (i == 1)
 		{
 			++i;
 		}
 		std::cout << i << std::endl;
 	}
 }
 // output:
 0
 2
I don't mean to say that Visual C++ is generally very good at keeping standards, but they sure have improved a lot in that respect, so I would wonder if they hadn't implemented such a relatively small check...
Bfroehler (talk) 08:17, 27 September 2011 (UTC)
thread_title: Immutable loop variable in C++?

message: As can be seen from the heading, the individual words "for" and "loop" are normal common words that could be read as having the normal meaning, not the special meaning of "for loop" with the words set off by quoting in the normal way. The first apeparance in the text outside of quotes is ...  the for loop  ... While I was looking at other stuff, not having read the article from the beginning and so not having read the sequence "for loop" to for loop, [see?] this seemed unnecessarily ambiguous. So I tried changing to for-loop to make a clearly compounded word. In other cases, such as e-mail, as time passes, email becomes common - though forloop still looks odd, and I still write e-mail from my mudhole where I am stuck. Since different computer languages use different terms, there is no single usage to follow in English text describing them. Humm... How about for loop for for loop [see?]? Anyway, I thought I'd protected the usage of W-links; if I'd mistakenly zapped any, they could have been repaired. NickyMcLean (talk) 20:41, 9 December 2012 (UTC)
replies: text: Well, in all the programming books and tutorials I have read I never encountered "for-loop" with a hyphen, it has always been "for loop" (and "while loop" and "do loop" and so on). But I don't feel very strongly about this, and I won't revert if you change "for loop" to "for-loop" again. However, please be careful to check your edit ("Show changes") before saving. — Tobias Bergemann (talk) 08:50, 11 December 2012 (UTC)
replies: text: The more correct form is for-loop with the hyphen (as well as while-loop, do-loop, do-while-loop, repeat-loop, foreach-loop, etc.), for exactly the reasons stated by NickyMcLean. The same goes for other constructs, such as if-then-else, do-while, repeat-until, try-catch, test-and-set, etc. It is indeed confusing to parse a sentence such as "for for loops with controlling expressions..." or "while while loops are preferred over for loops for simple testing, for more control, for loops are preferred." Consequently, I have changed all of the occurrences in the article to use the hyphenated form. That being done, note that the title of this article should still remain as "For loop". — Loadmaster (talk) 17:59, 7 May 2015 (UTC)
thread_title:  For-loop for loop? 

message: Some versions of BASIC (various flavors from Microsoft, like QuickBASIC and Visual BasicMSDN documentation on this, for Visual Basic: https://msdn.microsoft.com/en-us/library/5z06z1kb.aspx) offer an optional STEP keyword on for-loops:
 REM this will print 1, 3, 5, 7, 9
 FOR i = 1 to 10 STEP 2
     PRINT i
 NEXT
Since several of the language examples discuss step syntax, it should probably be mentioned here, too.
Ryancgordon (talk) 21:16, 25 September 2015 (UTC)
thread_title:  BASIC "step" keyword 

message: Duplication makes it harder to pick details about for loops (for example, there a timeline at this page). IMO it is better to define loops in single place (Control flow) and then only refer to this page. Ushkin N (talk) 05:04, 22 May 2016 (UTC)
replies: text: Oppose merge, on the grounds that the current summary/main format seem to work well  (that is, that Control flow#Count-controlled loops contains a template:main to For loop) and that a merge page would be too long and unbalanced towards for loops. Klbrain (talk) 16:44, 28 February 2018 (UTC)
replies: text: Closing, given no support for the merge over almost 2 years, and no comment on the "oppose" over the course of month. Klbrain (talk) 16:36, 29 March 2018 (UTC)
thread_title:  Merge with Control_flow#Loops 

message: The section marked COBOL 1960 - uses an in-line PERFORM VARYING and an END-PERFORM.  These constructs didn't exist in 1960.  It was around 1980 that these became part of the language.  Anyone care if I make an edit?  — Preceding unsigned comment added by 206.248.171.144 (talk) 17:33, 12 July 2020 (UTC)
thread_title:  COBOL Performs CIRCA 1960 

message: The for loop is used when the number of iteration are known before the loop entered 103.7.79.52 (talk) 14:53, 29 November 2021 (UTC)
replies: text: Or not -- e.g.: for (;;) {do something forever}. Wtmitchell  (talk) (earlier Boracay Bill) 14:56, 29 November 2021 (UTC)
thread_title:  Computer 

message: The section Crystal (1993) seems to be referring to the programming language Lua, which was released in 1993 and which is the only language I know that uses pairs and ipairs functions for looping.  The only language called Crystal (programming language) I can find was created in 2014.  If this is more than just a simple mistake, then it should be explained in the article. Sam A Hill (talk) 04:58, 6 December 2023 (UTC)
thread_title:  Should Crystal (1993) be Lua? 

message: C's for loop is a very poor example and should not be given pride of place.Control structures should be semantic, yet C's for loop is a level below that where the programmer must increment the control variable. Here is a further explanation.
The for loop takes a range variable and loops on that range. The range can be explicit as in:
for I := 1, 4, 7, 34, 78, 97 do …
or specified by bounds:
for I := 0 until 9 do …
or the steps may be specified:
for I := 0 step 2 until 9 do …
Note how the for loop handles the increments for you.
There is a primitive form where the programmer must do this detail as in
for (I := 0, I <= 9, I++) …
(Also note syntactically that the two characters ‘do’ that are easy to type have been replaced by the harder to type ‘( )’.)
This is a primitive low-level form of for and is inferior to the more semantic forms above. What we are interested in with syntactic forms is the meaning of the construct as in the first three, not the mechanics of the loop as in the more primitive form. Ian.joyner (talk) 04:59, 12 April 2024 (UTC)
thread_title:  C's for loop is primitive