-8

I was searching for an explanation for the "double semicolon" in the code:

for(;;){}

Original question.

I do not have enough reputation to even leave a comment so I need to create a new question.

My question is,

What does an "extra" semicolon means. Is this "double semicolon" or "extra semicolon" used for "something else"?

The question comes from a person with no knowledge of programing, just powered on my first arduino kit and feel like a child when LED blinked. But it is the reality that the questions, like general occurence, are radiating the actual knowledge of the person asking the question.

And beyond "personal preference" in using while(1) or for(;;) is it helpful in using for(;;) where you do not have enough room for the code itself?

I have updated the question. Thank you for the quick explanation. You opened my eyes with the idea of not using anything in for loop = ). From basic high school knowledge I am aware of for loop so thank you for that.

So for(;;) returns TRUE "by default"?

And my last line about the size of the code?

Or it is compiled and using for or while actually does not affect the compiled code size?

titus
  • 377
  • 4
  • 16
  • 2
    This is the syntax of `for` statement. `for(something; something; something)`. `something` can be empty. – Eugene Sh. May 24 '18 at 17:23
  • I think the question should not be flaged as a duplicate. This is a completely separate question from the one in URL. The answer there was not fully complete in my opinion because only now I am more clear with some details. Also the second part of the question about the size of the code. But I do not mind if this stays flagged. – titus May 24 '18 at 17:37
  • Yes, it has no effect on size of the executable. – HolyBlackCat May 24 '18 at 17:43

1 Answers1

0

A for loop is often written

int i;
for (i = 0; i < 6; i++) {
    ...
}

Before the first semicolon, there is code that is run once, before the loop starts. By omitting this, you just have nothing happen before the loop starts.

Between the semicolons, there is the condition that is checked after every loop, and the for loop end if this evaluates to false. By omitting this, you have no check, and it always continues.

After the second semicolon is code that is run after every cycle, before that condition is evaluated. Omitting this just runs additional code at that time.

The semicolons are always there, there is nothing special about the face that they are adjacent. You could replace that with

for ( ; ; )

and have the same effect.

While for (;;) itself does not return true (It doesn't return anything), the second field in the parentheses of the for always causes the loop to continue if it is empty. There's no reason for this other than that someone thought it would be convenient. See https://stackoverflow.com/a/13147054/5567382. It states that an empty condition is always replaced by a non-zero constant, which is the case where the loop continues.

As for size and efficiency, it may vary depending on the compiler, but I don't see any reason that one would be more efficient than the other, though it's really up to the compiler. It could be that a compiler would allow the program to evaluate as non-zero a constant in the condition of a while loop, but recognise an empty for loop condition and skip comparison, though I would expect the compiler to optimize the while loop better. (A long way of saying that I don't know, but it depends on a lot.)

Thomas Jager
  • 4,836
  • 2
  • 16
  • 30
  • I will mark this answer. If you please can read my updated post and possibly add it to your answer. – titus May 24 '18 at 17:38
  • @titus I've update my answer with some information. I don't have a good answer on code size because that depends on so much. – Thomas Jager May 24 '18 at 17:50