Two semicolons inside a for-loop parentheses

osse picture osse · Apr 19, 2013 · Viewed 27.2k times · Source

I'm customising a code I found over the internet (it's the Adafruit Tweet Receipt). I cannot understand many parts of the code but the most perplexing to me is the for-loop with two semicolons inside the parentheses

boolean jsonParse(int depth, byte endChar) {
  int c, i;
  boolean readName = true;

  for(;;) {  //<---------
    while(isspace(c = timedRead())); // Scan past whitespace
    if(c < 0) return false; // Timeout
    if(c == endChar) return true; // EOD

    if(c == '{') { // Object follows
      if(!jsonParse(depth + 1, '}')) return false;
      if(!depth) return true; // End of file
      if(depth == resultsDepth) { // End of object in results list

What does for(;;) mean? (It's an Arduino program so I guess it's in C).

Answer

taocp picture taocp · Apr 19, 2013
for(;;) {
}

functionally means

 while (true) {
 }

It will probably break the loop/ return from loop based on some condition inside the loop body.

The reason that for(;;) loops forever is because for has three parts, each of which is optional. The first part initializes the loop; the second decides whether or not to continue the loop, and the third does something at the end of each iteration. It is full form, you would typically see something like this:

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

If the first (initialization) or last (end-of-iteration) parts are missing, nothing is done in their place. If the middle (test) part is missing, then it acts as though true were there in its place. So for(;;) is the same as for(;true;)', which (as shown above) is the same as while (true).