Search code examples
loopsfor-looplanguage-agnosticprogramming-languageswhile-loop

Which languages support while or do/while loops with automatic iteration indexing?


As the title suggests, which languages support while or do/while loops with automatic iteration indexing?

In other words a [while] or [do/while] loop that provides the iteration index automatically without having to resort to an intrinsically indexed construct such as the [for] loop. This was an odd question to Google which fetched out of context results.

Take the following as an example for C#:

int count = 0;
while (count < 10)
{
    Console.WriteLine("This is iteration #{0}", count++);
}

As opposed to the following fictitious loop:

while<int> (value < 10)
{
    Console.WriteLine("This is iteration #{0}", value); // Or value-- for that matter.
}

I do not know much about language design and thus the question. the [for] loop has a lot of flexibility but each kind of loop is best suited for certain scenarios. Still, it makes certain scenarios very odd such as combining iterators with indexers.

This is not meant to be an open-ended question. Simply, Do any languages support such constructs, and if not, ummm, cannot ask that here as that would make it open ended

UPDATE: I do realize the complexity that would arise out of nesting such loops but am sure that can be circumvented by some clever naming convention.

Something I had in mind but did not mention was the use of a clever lambda expression in the case of C# for example. That would not be an addition to the language but merely an extension (which I believe is only valid for reflection-friendly platforms such as .NET and Java).


Solution

  • In ruby: array.each_index{|i| print i } will go through each index.