@[email protected]
link
fedilink
-3
edit-2
2Y

I disagree. It’s a while loop, because a for-loop is finite, so you can’t count to infinity with it.

There’s nothing special about a generic for loop (at least in C-like languages). There’s no reason you couldn’t do something like for (i = 0; true; i++) to make it infinite. Some languages even support an infinite list generator syntax like for i in [0..] (e.g. it lazily generates 0, then 1, then 2, etc. on each iteration) so you can use a for-each style loop to iterate infinitely.

Now, whether or not you should do such things is another question entirely. I won’t pretend there aren’t any instances where it’s useful, but most of the time you’re better off with a different structure.

Pitri
link
fedilink
102Y

there is no reason for a (non-foreach) for loop to be any more or less finite than a while loop.

for (a; b; c)
{
  d;
}

is just syntactic sugar for

{
  a;
  while (b)
  {
    d;
    c;
  }
}

in most or all languages with c-like syntax.

for (i=0; true; i++)

I wanna see how you get a while loop to actually go to infinity. I’ll wait…

on second thought, no I won’t.

Create a post

Rules:

  1. Be civil and nice.
  2. Try not to excessively repost, as a rule of thumb, wait at least 2 months to do it if you have to.
  • 1 user online
  • 828 users / day
  • 79 users / week
  • 904 users / month
  • 2.75K users / 6 months
  • 0 subscribers
  • 2.97K Posts
  • 31.5K Comments
  • Modlog