5

In JS I stumbled across a kind of for loop which is for(;;) that functions like a while(true) loop. What do the semicolons function in the brackets of this for loop?

1
  • what answer you accept? there are a few good answers
    – Ori Marko
    Commented Jun 11, 2017 at 13:20

7 Answers 7

7
for (statement 1; statement 2; statement 3) {
    code block to be executed
}

Statement 1 is optional and is executed before the loop (the code block) starts.

var i = 0;
var length = 10
for (; i < length; i++) { 

    //The for loop run until i is less than length and you incerement i by 1 each time. javascript doesnt care what you do inside, it just check whether you have variable with name i and length
}

Statement 2 is again optional defines the condition for running the loop (the code block).

var i = 0;
var len = 100;
for (i = 5; ; i++) { 
    //Here you are just initializing i with 5 and increment it by 1 there is no break condition so this will lead to an infinite loop hence we should always have a break here somehwere.
}

Statement 3 is optional and is executed each time after the loop (the code block) has been executed.

var i = 0;
var length = 100;
for (; i < length; ) { 
    //Here you are just checking for i < length which is true. If you don't increment i or have an break it will turn into infinite loop
}

In nut shell when you have no conditions or initialization's it turns into an infinite loop.

4

Usually, a for loop header contains 3 parts:

for (var i = 0 ; i < 10 ; i++)
//   ^^^^^^^^^   ^^^^^^   ^^^

You first initialise a variable, check the condition and if it is true, do whatever the loop body says, then increment i.

What you might not know is that any part of the for loop header can be omitted. If the first part is omitted then no variable is initialised. If the second part is omitted then there is no condition check. It will always assume the condition is true.

So for(;;) is basically an infinite loop because it omitted the condition part of the for loop header. Every time, the condition is true, so it continues on forever.

3
for ( init; condition; increment )
{
   statement(s);
}

Here is the flow of control in a for loop:

The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.

Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.

After the body of the for loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again testing for a condition). After the condition becomes false, the for loop terminates.

3

The for loop consists of three parts:

for (INITIALIZATION; CONDITION; AFTERTHOUGHT)
{
}

If you omit these parts the whole will be evaluated as:

for(; true ;)
{ }

The initialization and after thought are just ignored.

3

First semi semicolon ends initialization statement.

Second semi semicolon ends condition check statement.

In your case both statement are empty (no initializing and checking nothing every loop).

1

Well I'm just going to take a guess. Typically a for loop might be like this:

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

so basically

for (;;)

is well forever, because loop condition is always met.

0

What I can still add is:

  • reference to the source,

    This conditional test is optional. If omitted, the condition always evaluates to true.

  • we can leave any of these three blocks in the loop for,

  • information that this behavior of the loop for is similar like in in C language and others with syntax inspired by C, such as C++, Java etc.,
  • a practical example of using such a loop:

let i = 1;

for (;;) {
  if (i === 4) {
    break;
  }
  console.log(i++);
}

  • I will also add that the statment block after the for loop is also optional (we only need to put the ; at the end). To show this, here's the next example: equivalent to the above code, but without the block statmant in the for loop:

for(var i = 1; i < 4; console.log(i++));

Not the answer you're looking for? Browse other questions tagged or ask your own question.