26
\$\begingroup\$

For loops are used extensively in many languages, but what would you do if no languages supported them?

Create a way to execute a basic for loop without using any repetition structures (for, foreach, while, do, etc).

The basic for loop that you need to replicate is set up like this

for(i=0;i<1000;i++)

You must be able to replicate this without using repetition structures. It must also execute code in your language that would be in the body of the loop. Any form of eval is allowed, though it may not execute the for loop on its own.

You can test your code by having it print i with a space 100 times, add this test case to the end of your answer to verify your code with others.

There are no restrictions on what exactly it has to be, all it needs to do is replicate the for loop.

Winner will be decided based on upvotes at the time that it is chosen.

\$\endgroup\$
4
  • \$\begingroup\$ 100 times or 1000? Or 100 000 times? \$\endgroup\$ Feb 19, 2011 at 6:50
  • 11
    \$\begingroup\$ I find the problem too undefined. You say “without using any repetition structures” and only give examples (but not an extensive list) of such prohibited structures. The answers provided so far use recursion or goto, both of which I would classify as “repetition structures”, but it can be debated. Without a proper definition of what is allowed and what isn’t, this question is not interesting. \$\endgroup\$
    – Timwi
    Mar 8, 2011 at 14:34
  • \$\begingroup\$ gotos, recursion... \$\endgroup\$ Mar 10, 2011 at 2:29
  • \$\begingroup\$ I am voting to close this as off-topic because we require objective validity criteria for all challenges. As it stands, there is no clear way to determine the validity of a submission. Additionally, there is no specification of the behavior of a valid submission. \$\endgroup\$
    – user45941
    May 20, 2016 at 5:35

61 Answers 61

1 2
3
0
\$\begingroup\$

Groovy, 32 bytes

(0..1000).each{ print "${it} " }
\$\endgroup\$
2
  • 1
    \$\begingroup\$ each is foreach => disqualified :) println is printing with newline, not space => disqualified. ;) \$\endgroup\$ Feb 19, 2011 at 7:20
  • \$\begingroup\$ Which is silly. Iteration is not a loop even though they contain 4 letters which are similar. Iteration = processing data in memory = what computers are built to do. \$\endgroup\$
    – user615
    Feb 19, 2011 at 14:11
1 2
3

Not the answer you're looking for? Browse other questions tagged or ask your own question.