Jump to content

Talk:Foreach loop

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

Display problem

[edit]

I see the second line of code (DoSomething) as in a box with a blue dotted outline (on FireFox 9.2). Is this normal? It seems to appear whenever I put in 4 spaces at the front of the line to simulate a tab.

Never mind...should have just looked through the editting guidelines. Got it.

Eshear

Proposal to merge into For loop

[edit]

I'm proposing that this article be merged into the For loop article. Please discuss on that article's talk page. — Dmeranda 17:06, 17 May 2006 (UTC)[reply]

Was turned down - see Talk:For loop --Krischik T 14:18, 1 February 2008 (UTC)[reply]
Sorta agree and disagree. IMO foreach loop, for loop, while loop, and do while loop should be merged into Loop (statement). The constructs are not clearly separatable into the current categories. For example, the 3-part c-style for loop is hardly a for loop! It's a while loop with spots for init and exit logic. Stevebroshar (talk) 13:50, 11 September 2025 (UTC)[reply]

Order of iteration

[edit]

It is common for a foreach style iteration not to guarantee any particular order of iteration, as comparted to indexed iteration. I think this should be mentioned? putgeminmouth 02:47, 5 May 2007 (UTC)[reply]

I think it depends on the language. In C#, the foreach statement is just a syntactic sugar for which an ordinary loop with an enumerator object manipulated is emitted, so the order of iteration is given by implementation of the underlying enumerator (of couse, if you take an array, stack or a list, their enumerators will enumerate objects in the order you would expect :-)). --Ondrejsv 21:21, 7 July 2007 (UTC)[reply]

Does Smalltalk support foreach nativly?

[edit]

Smalltalk support for "foreach" seams to be a border line.

While for C++ I think it is clearly an "unsupported" as language does not support foreach and it is emulated by libraries.

Smalltalk too implements foreach as library function. However there is difference: In Smalltalk all control structures are implemented as library features - so I am inclined to see Smalltalks do: [ :each as first class language feature.

--Krischik T 13:51, 1 February 2008 (UTC)[reply]

Flash (ActionScript) has a function for(... in ...) . --Hkhk59333 (talk) 03:43, 19 March 2009 (UTC)[reply]

PHP

[edit]

What does qualify the word idiosyncratic in the PHP part?

PHP has an idiosyncratic syntax

Not that I care about PHP, but it doesn't sound very neutral to me, which I think it should.

--Rfc (talk) 23:07, 29 March 2009 (UTC)[reply]

Use of Python range() in loops

[edit]

The statement, Several languages, including Tcl and Python, have only a foreach loop, requiring explicit counting to achieve "standard" for behavior. is a bit troublesome to me. In Python, the common idiom

for i in range(n):

isn't what I would call explicit counting. This can be remedied by adding something like, or language-specific idioms, but by the time you're really said it correctly, it's gotten kind of complicated. I'm wondering if maybe it's better to drop the entire statement? -- RoySmith (talk) 19:51, 6 June 2009 (UTC)[reply]

Hello, 2009. I removed the statement altogether, because it's false. Python doesn't have "only" a foreach loop, as it has the common while loop which is only syntactically different from for-looping. 212.68.15.66 (talk) 07:29, 23 February 2011 (UTC)[reply]

C++ examples

[edit]

I think it would be better to have an example using Boost foreach than QT. Having both would be fine, but might make the section too long.

These are the reasons a Boost example would be more useful to the average interested party:

  • A single Boost library is a lot smaller to integrate into your project than all of QT.
  • The example shouldn't use strange QT syntax to construct the list or qDebug(), but something that all C++ programmers would know.
  • QT makes a copy of the container, which is fine for QT containers but is expensive in general.
  • QT makes a copy of the container, so modifications to the iterated object are silently lost, whereas Boost foreach can mutate the container. — Preceding unsigned comment added by 74.85.42.110 (talk) 01:51, 7 September 2011 (UTC)[reply]

C

[edit]

These statements:

The macro is unhygienic: it declares a new variable in the existing scope which remains after the loop.
One foreach macro cannot be defined that works with different collection types (e.g., array and linked list) or that is extensible to user types.

are incorrect. In C99 and higher one may use declaration of type inside the loop statement - so variable's scope is limited to the loop. Also, as macro is considered a form of non-recursive templates, it is possible to implement one macro for various types of collection as long as there is a common interface and some naming conventions are hold.

For example if each implemented collection of some type we can define a function type *type_first(type *collection) and type *type_next(type *collection, type *current). Then one may iterate with:

   #define FOREACH(TYPE, NODE, COLLECTION) \
   for ( \
       TYPE *NODE = TYPE ## _ ## first(COLLECTION); \
       NODE; \
       NODE = TYPE ## _ ## next(COLLECTION, NODE) \
   )

Please move

[edit]

Requested move 21 May 2016

[edit]
The following is a closed discussion of a requested move. Please do not modify it. Subsequent comments should be made in a new section on the talk page. Editors desiring to contest the closing decision should consider a move review. No further edits should be made to this section.

The result of the move request was: Not moved as consensus to keep the article at it's current name has been established. (non-admin closure) Music1201 talk 17:29, 5 June 2016 (UTC)[reply]



Foreach loopComparison of programming languages (foreach loop) – Consistent names in Category:Programming language comparisons 128.70.197.164 (talk) 23:00, 21 May 2016 (UTC)--Relisted. InsertCleverPhraseHere 06:10, 29 May 2016 (UTC)[reply]


The above discussion is preserved as an archive of a requested move. Please do not modify it. Subsequent comments should be made in a new section on this talk page or in a move review. No further edits should be made to this section.

Iterative for loop listed at Redirects for discussion

[edit]

An editor has asked for a discussion to address the redirect Iterative for loop. Please participate in the redirect discussion if you wish to do so. — Arthur Rubin (talk) 10:56, 19 May 2019 (UTC)[reply]

Rename to 'For each'

[edit]

There's an awkward distinction between general concept name and programming language syntax. Many languages use a keyword like foreach, but some use different syntax for the same semantics. This article seems to be about the semantics; not the syntax. So what to name the article? I think 'foreach' is not good since it seems to prefer certain language syntax. I think 'for each' would be better as it's more agnostic. And no, not for-each since that's lame. Stevebroshar (talk) 13:55, 11 September 2025 (UTC)[reply]

Merge all loops

[edit]

Regardless of my comment to rename this article, I think foreach loop, for loop, while loop, and do while loop should be merged into Loop (statement). The constructs are not clearly separatable into the current categories. For example, the 3-part c-style for loop is hardly a for loop! It's a while loop with spots for init and exit logic. Stevebroshar (talk) 13:56, 11 September 2025 (UTC)[reply]

I'm in two minds over this. I'd support merging the test-based loops, do-while and while-do, and the deterministic iterators for and foreach into two groups. Should those two be merged into one article? Maybe, but it's then a harder article to write and make it clear. It's useful to have all the related content in one place, but the single article would still need to preserve the clear conceptual distinction between the two of them (and the traces of the implementation details that still hang on with a few platforms). But WP has historically been poor at writing well on software topics, so could we really make that happen? Would it be good or better? Or just a mess that lumps all four together. Andy Dingley (talk) 16:24, 11 September 2025 (UTC)[reply]
Thanks for the reply. ... Oh, I've seen many terrible merges. Many articles have been merged that shouldn't have been. And, more to your point, execution of the merge is often lazy and bad! Copy and paste is not the way. Merging requires effort to get a good result. I'm good at it :) ... I think sometimes people suggest a merge when they don't understand the difference between two things. But, this is not that. The issue here is that that categories of loop statements are not easily separatable into a neat set. And there are lots of categories. And each one really isn't that complicated so doesn't need all that much description. And we don't need an example of each type in each language! And much is lost in segregating the info. The info could be much more concise and useful when grouped together. And, the current article names suck! We're naming articles based on syntax, but clearly we're talking about common semantics. My point is that it's a mess and needs an overhaul. And I think a single article is the way to go. Stevebroshar (talk) 15:28, 13 September 2025 (UTC)[reply]
One more thing: there's info in control flow about loops that would fit nicely in Loop (computer programming) and is too specific to belong in an article about control flow. Stevebroshar (talk) 15:35, 13 September 2025 (UTC)[reply]

Fewer examples

[edit]

IMO three examples of anything is enough. Sometimes there's reason for more, but 50 (approximately, I got bored of counting). OMG. That's _way_ to many. WP is not a programming tutorial. Stevebroshar (talk) 15:31, 13 September 2025 (UTC)[reply]