By osse

2013-04-19 20:38:47 8 Comments

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).


@Suraj Jain 2017-02-16 15:55:43

Here's What Wikipedia Says About it

Use as infinite loops
This C-style for-loop is commonly the source of an infinite loop since the fundamental steps of iteration are completely in the control of the programmer. In fact, when infinite loops are intended, this type of for-loop can be used (with empty expressions), such as:

for (;;)
//loop body

This style is used instead of infinite while(1) loops to avoid a type conversion warning in some C/C++ compilers.Some programmers prefer the more succinct for(;;) form over the semantically equivalent but more verbose while (true) form.

@taocp 2013-04-19 20:39:29

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).

@technosaurus 2019-04-27 17:34:54

Or...#define ever ;; so you can write for (ever){ ... }

@Travis J 2013-04-19 20:42:25

Each clause of a for loop is optional. So when they are excluded, it still loops. for loops compile into while loops.

The end result becomes a check to initialize any variables, which concludes after nothing happening since it is empty, a check to the boolean condition in the second clause, which is not present so the loop starts, and once the loop hits the end bracket, a check to see if there is any code to run before checking the boolean condition again.

In code it looks like:



@Fls'Zen 2013-04-19 20:42:09

The for loop has 3 components, separated by semi-colons. The first component runs before the looping starts and is commonly used to initialize a variable. The second is a condition. The condition is checked at the beginning of each iteration, and if it evaluates to true, then the code in the loop runs. The third components is executed at the end of the loop, before another iteration (starting with condition check) begins, and is often used to increment a variable.

In your case for(;;) means that it will loop forever since the condition is not present. The loop ends when the code returns or breaks.

Related Questions

Sponsored Content

10 Answered Questions

40 Answered Questions

[SOLVED] Loop through an array in JavaScript

6 Answered Questions

[SOLVED] How to iterate through two lists in parallel?

41 Answered Questions

[SOLVED] How do I loop through or enumerate a JavaScript object?

11 Answered Questions

[SOLVED] A 'for' loop to iterate over an enum in Java

5 Answered Questions

[SOLVED] Get loop count inside a Python FOR loop

  • 2010-07-01 22:59:27
  • greye
  • 373301 View
  • 275 Score
  • 5 Answer
  • Tags:   python for-loop

9 Answered Questions

[SOLVED] Why should I use a semicolon after every function in javascript?

  • 2009-12-02 17:51:50
  • macca1
  • 73957 View
  • 284 Score
  • 9 Answer
  • Tags:   javascript syntax

5 Answered Questions

7 Answered Questions

[SOLVED] How to use double or single brackets, parentheses, curly braces

  • 2010-02-02 22:22:54
  • Tim
  • 375273 View
  • 681 Score
  • 7 Answer
  • Tags:   bash syntax

Sponsored Content