6

I'm working on some C# code that has loop syntax that I've never seen before:

for (;;)
{
  //Do some stuff
}

What does a for loop without a init; condition; or increment do? By the way it's really hard to find meaningful search results on the internet for "for (;;) c#" on any search engine I tried.

-Eric

10
  • 7
    If null is null then keep going. At the end , null + null is how I've always read. It's an infinite loop, if I'm not mistaken Commented Jan 17, 2019 at 22:16
  • 1
    So it goes unless there's a break, throw, or something? Commented Jan 17, 2019 at 22:17
  • 1
    @Symon your comment is actually an answer. Please post it as an answer so we can upvote it :) Commented Jan 17, 2019 at 22:19
  • 2
    The empty condition statement is the real key here. Without a condition, the loop will run forever unless something inside the block terminates it. I believe it's generally considered more intentional to write while(true) because that explicitly states the infinite-loop condition (and while conditions are not optional). Commented Jan 17, 2019 at 22:30
  • 1
    It was semi-idiomatic in the early days of C. (I can remember a "cute" macro someone wrote that looked like #define EVER ;; that allowed you to write for(EVER) { /* code */ }). I remember seeing it a lot in 80's era C code, not so much in early 90s C and C++ code and very rarely in the last decade or two. Yeah, I'm that old, but I'm guessing the person who wrote the code you are looking at is old as well Commented Jan 17, 2019 at 23:14

4 Answers 4

12

That is an infinite loop. Like you stated, it will run until a part of it breaks (throws an exception or otherwise exists the loop) or the machine runs out of resources to support the loop.

for (;;)
{
   //do stuff
} 

Is just the same as:

do
{
   //do stuff
}while (true)

while(true)
{
   //do stuff
}
Sign up to request clarification or add additional context in comments.

5 Comments

There is no variable to set to null. Refer to the spec: Parts 1 and 3 ("for_initializer" and "for_iterator") are empty statements and do nothing. Part 2 ("for_condition") is a special case, where the lack of a statement is specified to be treated as if the condition evaluated true.
@Symon To be clear, in the context I linked, the ? indicates an optional symbol in the ANTLR grammar C# compilers use to parse C# source code. There are no C# variables here.
@JoeSewell, ahh. That makes more sense. That must be where my confusion of it must've been.
@RufusL , I see. Read more of the documentation on it and I understand where I was wrong. Thanks for clearing it up!
Sure, no problem. Thanks for fixing the answer +1
9

The syntax of a for loop is thus:

for (condition; test; action)

Any one of those items can be omitted (per the language spec). So what you've got is an infinite loop. A similar approach:

while (true) { // do some stuff }

8 Comments

For reference, the C# draft specification on the for statement. Notice the "If the for_condition is not present or if the evaluation yields true, control is transferred to the embedded statement." (emphasis mine).
Calling the first argument condition is confusing, and the third one action is vague; the terms from the docs - for (initializer; condition; iterator) - tell a better story.
@mklement0 I don't remember why I put "condition" there, so I agree with you on that, but the reason why I put "action" is because you can do other things besides increment a variable.
While it's possible to do other things in the iterator section, its primary purpose is to iterate, so I think it's better to call it iterator - while perhaps adding a note that points out that you're technically not restricted to incrementing a loop variable.
Where in your link does it say that that is its primary purpose?
|
8

for (;;)

Short answer: It is an infinite loop which is equivalent to while(true)

Long answer: for (initializer; condition; iterator) Structure of the for statement

  • initializer block: Do not initialize variable.
  • condition block: with no condition (means execute infinitely) => while true
  • iterator block: with no operation to any variable (no iterator)

for(;;) example from official documentation

Comments

4

This type of for loop is an infinite loop. It is the equivalent of while(true){stuff to be executed...}. It keeps on going until it hits a break, return, or a goto to a label outside the loop.

A for loop has three parts, an initialization, a condition, and a block to be executed after the loop. Without a condition to be tested against, the loop will just keep on going.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.