Talk:For loop

From Wikipedia, the free encyclopedia

Contents

[edit] 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)

I would tend to agree. We should use something that isn't math-specific. --Hooperbloob 19:11, 21 January 2006 (UTC)
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)

[edit] 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)

[edit] 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)

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)

They are two similar but distinct concepts, it would confuse anyone who didn't understand the differences. —The preceding unsigned comment was added by 82.41.27.90 (talk • contribs) 20:23, 15 July 2006 (UTC)

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. —The preceding unsigned comment was added by 69.95.130.62 (talk • contribs) 20:21, 20 July 2006 (UTC)

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 (talkcontribs) 04:16, 25 August 2006 (UTC)
What languages other than PHP actually use the term foreach anyway? —Simetrical (talkcontribs) 04:41, 25 August 2006 (UTC)
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)
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 (talkcontribs) 21:29, 3 September 2006 (UTC)

[edit] Fortran has both 'DO' and 'FOR' loops

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

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)