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]

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]

  • Oppose There's nothing wrong with the current name. This is (yet another) foolish consistency to no purpose. In particular, the existing names are the wrong way round and should be disambiguated (if even necessary) in the form "foreach loop (comparison of programming languages)" instead. Andy Dingley (talk) 23:11, 21 May 2016 (UTC)[reply]
  • Oppose as above. Dicklyon (talk) 05:09, 30 May 2016 (UTC)[reply]
  • Oppose WP:CONCISE. There is no need to move every single article on aspects of programming languages to "comparison of programming languages" merely because it lists many different languages. 210.6.254.106 (talk) 05:51, 31 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]