Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For (;;) loop explanation

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?

like image 929
tnkh Avatar asked Jun 11 '17 07:06

tnkh


3 Answers

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.

like image 196
utkarsh31 Avatar answered Oct 11 '22 17:10

utkarsh31


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.

like image 4
Sweeper Avatar answered Oct 11 '22 17:10

Sweeper


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.

like image 3
Sumit Deshpande Avatar answered Oct 11 '22 17:10

Sumit Deshpande