Can someone explain me the Syntax of a for-loop like "for (;;)" what i need to know is whether the header of a for-loop like "for (;;)" is having empty-statements or not.
I searched the ECMAScript specification about what will happen if all the optional expressions within the for-loop's header is skipped like for (;;) in the specification but i still didnt find about it
can someone explain me about this even the specification haven't mentioned that a for-loop like for (;;) loops/runs infinite times
and i need to know one last thing why people call the header of a for-loop is having Expression's i see that the syntax of a for loop allows us to write declarations like var i = 0 in the header of the for-loop and i see the for-loops syntax allows us to write semicolons ; in its header only statements require semicolons does that mean all the syntax within the for-loop's header is having Statements
In a standard for loop definition, it would go as follows:
for (var i = 0; i < 5; i++)
{
//Do something here
}
You have your statement being defining i as 0 (var i = 0). Then, there is the condition/expression that the for loop checks each time as it runs, which in this example checks that i is less than 5 (i < 5).
So, to address the first part of your question, when you define a for loop as for(;;), you are simply defining a loop with no condition that must be met. Therefore, it continues to run/loop forever. This can be done because all parts of the for loop definition are optional. So, there are no "empty-statements". It is simply defining the for loop without the optional arguments. We fill it in with semicolons instead in order to represent the transition between the empty arguments.
Related
I'm curious why when making a for loop in JavaScript a ; is not necessary after the i++ statement? It tripped me up. It actually doesn't even work if you include a semicolon after the iterator statement.
e.g.
for(var x=1; x<100; x++){
document.write(x);
}
While tokenizing the code for compiling, the JS-parser usually knows where statements start and end due to Automatic Semicolon Insertion.
In the case of a for-loop however, semicolons are interpreted as a separator of statements. For-Loops accept up to three "parameters". If you put in a semicolon, a fourth one will be expected and it will throw an error.
The final-expression-statement of a for-loop is terminated by ).
This control-structure behaves similar to PHP, C, Java and C++
thanks to #Antoniu Livadariu for mentioning this in the comments
Further Information
MDN - Lexical Grammar
MDN - The "for" Statement
thejameskyle's super tiny compiler
I'm studying w3schools regarding semicolon placement (under the heading: "Statement Rules") and they said "Always end a simple statement with a semicolon." And: "Do not end a complex statement with a semicolon." They did not define what the difference between a simple and complex statement is and a thorough online search did not provide an answer either. So I am still confused with JavaScript semicolon placements. I would appreciate any help.
A complex statement is a sequence of ('simple') statements inside {}.
So you'd write (simple statement):
a = 3;
and (complex statement, in many languages called a compound statement, in JavaScript called a BLOCK)
{
a = 3;
b = 4;
}
but not
{
a = 3;
b = 4;
};
A simple statement in JavaScript is a single line of code that performs a specific task and does not require any additional statements to complete its execution. Example: x = 10.
A complex statement, also known as a compound statement, is a group of simple statements that are executed together as a single unit and controlled by a compound statement like if-else, for, while loop, etc. Example:
if (x > 10) {
x = 0;
y = 1;
}
Complex statement refers to blocks like in if, for, while etc.
if(condition){
} //no semicolon
Simple statement:
var x = 2;
In some cases, JavaScript allows you to omit the semicolon at the end of a statement. In other cases, it has to be there, or the next line will be treated as part of the same statement. The rules for when it can be safely omitted are somewhat complex and error-prone.
i recommend you check this Link
I am definitely a novice when it comes to JavaScript. I have searched around stackoverflow and the web for an explanation as to why this JavaScript for loop does not loop infinitely, but I have yet to find a clear answer. I am currently using this loop, a loop that I borrowed and modified, on my website and it is working as intended; however, I would like to know why it isn't looping infinitely. I was under the impression that the first statement of a for loop defines the loop variable, the second sets the looping condition (which prevents an infinite loop), and the third modifies the loop variable after the code block has been executed. Why is this loop not looping infinitely if it lacks a loop condition statement?
function showAll() {
var aBoxes = document.querySelectorAll('.aBox');
for (var i = a.Boxes.length; i--;) {
aBoxes[i].style.display = 'block';
}
}
When the variable "i" is finally decremented to 0, the value of i-- will be "falsy" and the loop will terminate. The loop does not lack a condition expression; that's what i-- is.
As long as i--; is not 0, it will be evaluated to true, hence the loop will continue. When it reaches 0, which evaluates to false, it will stop
Because it doesn't lack a loop condition statement. It lacks the modifying statement. One lacking the second condition would look like this:
for (var i = a.Boxes.length; ; i--)
What your loop is doing is decrementing i whenever it's time to check the loop condition statement and then checking to see if the resulting value is true or false (i.e. non-zero or zero). When i gets small enough (i==0) this will terminate the loop.
The loop isn't missing the condition expression; it's missing the expression that occurs at the end of each loop iteration (the third expression). However, i is decremented as a side-effect of the condition expression, so a final expression is not needed.
The condition expression evaluates to false once i reaches 0 and so the loop terminates.
More generally, each of the expressions in a for statement is optional. It is perfectly fine to omit any or all of them.
I came across a strange syntax for the for in an open source Javascript library.
for (;;) {
}
What does this even do? Can anyone answer?
It's "loop forever" and it works the same in C, C++, C#, Java etc. You've got a for statement with
no init step
no termination check (=> continue forever)
no end-of-loop step
It's an infinite loop, just like while(true).
As others have already pointed out, if will run forever, but you can still end the loop with break. Like:
for (;;) {
if (condition) {
break;
}
}
Mozilla
has a good breakdown of how javascript evaluates for loops:
for ([initialization]; [condition]; [final-expression])
statement
initialization
An expression (including assignment expressions) or variable declaration. Typically used to initialize a counter variable. This
expression may optionally declare new variables with the var keyword.
These variables are not local to the loop, i.e. they are in the same
scope the for loop is in. The result of this expression is discarded.
condition
An expression to be evaluated before each loop iteration. If this expression evaluates to true, statement is executed. This conditional
test is optional. If omitted, the condition always evaluates to true.
If the expression evaluates to false, execution skips to the first
expression following the for construct.
final-expression
An expression to be evaluated at the end of each loop iteration. This occurs before the next evaluation of condition. Generally used to
update or increment the counter variable.
statement
A statement that is executed as long as the condition evaluates to true. To execute multiple statements within the loop, use a block
statement ({ ... }) to group those statements.
Most relevant to this question is in the condition section:
This conditional test is optional. If omitted, the condition always
evaluates to true.
A simple/empty ; in many languages acts as a blank but true (and executable) statement.
Hence, in the for loop, all three expressions are true, always matched and hence, results in an infinite loop.
It has also been on Wikipedia. As per the wiki page
This style is used instead of infinite while(1) loops to avoid a
warning in Visual C++.
The controlling expression of an if statement or while loop evaluates to a constant. If the controlling expression of a while loop is a constant because the loop will exit in the middle, consider replacing the while loop with a for loop. You can omit the initialization, termination test and loop increment of a for loop, which causes the loop to be infinite (like while(1)) and you can exit the loop from the body of the for statement.
This is valid for loop for (;;) {} if condition is not given in for loop its by default true. So loop run infinitely. same as for (;1;) {}
Additionally not only in Javascript its true in C, C# language also etc.
Read this: Is “for(;;)” faster than “while (TRUE)”? If not, why do people use it? to know why some programmers may prefer for (;;) {} over simple while(true){} (interesting)
It runs an infinite loops, because
Essentially, a for loop is a shortened version of another loop, with this syntax
for(a;b;c){
}
is
a;
while(b){
c;
}
so loops forever, since the b is empty, and it has no increment operator to change anything.
Basically, it's an infinite loop of nothingness.
EDIT: Apparently, the empty command evaluates to true.
I've seen this in some JavaScript code but I don't get what it does.
for(;;){
//other code
}
I'm used to the for(i=0;i<someLength;i++){...} format but I'm stuck to figure out how or what the "(;;)" syntax is for?
In JavaScript, for (;;) { ... } just creates an infinite endless loop, which is almost exactly the same as:
while (true) {
// ...
}
or
do {
// ...
} while (true);
for (;;) is the exact same syntax. You're allowed to leave out parts that go between the semicolons, even all of them. If you leave the condition out, it's assumed to be true, so it results in an infinite loop. You can of course still exit with break or return or whatnot, hence making it not useless.
It creates a loop that runs indefinitely. It's the same syntax you're used to seeing, there's just no code between the semicolons.
The syntax for a for loop includes an initializer, followed by a semicolon, followed by a condition for continuing the loop, followed by a semicolon, followed by code to run after each iteration of the loop.
Since there is no initializer, no condition that ever evaluates to false, and no post-loop code, the loop runs forever.
That's an infinite loop. A for loop has 3 sections separated by ;: the initialization section where you normally declare and define your counter's starting conditions, the middle conditional statement that evaluates to true or false, and the increment section.
All 3 of these sections are optional. You can include any or none. The loop continues to loop until the conditional is false. If that condition is never met (since it was left out), it loops forever.
A place where you'll likely see it is prepended to JSON data as described by this SO post.
always true https://developer.mozilla.org/en/JavaScript/Reference/Statements/for