1

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

Kevin
  • 209
  • 2
  • 11
  • 1
    The [documentation at MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration) is quite clear. In general, programming documentation will put items in brackets `[]` when that item is optional. – Scott Marcus May 22 '20 at 15:34
  • 2
    An "empty" `for` runs forever because the abort condition is only evaluated when there is one. No condition to check -> No reason to stop. It's defined here [13.7.4.8 Runtime Semantics: ForBodyEvaluation ( test, increment, stmt, perIterationBindings, labelSet )](https://tc39.es/ecma262/#sec-forbodyevaluation) – Andreas May 22 '20 at 15:36
  • 1
    *does that mean all the syntax within the for-loop's header is having Statements* Yes. Declarations are expressions, statements are expressions, anything that needs to be evaluated is an expression. – Scott Marcus May 22 '20 at 15:37
  • 1
    @ScottMarcus i dont think declarartions are `Expressions` – Kevin May 22 '20 at 15:40
  • @Kevin Technically true. But, all declarations include expressions. – Scott Marcus May 22 '20 at 17:11
  • @ScottMarcus No, _Statements_ include expressions. _Declaration_ are exactly everything that are not _Statements_. See [the spec](https://tc39.es/ecma262/#prod-StatementListItem). – Sebastian Simon May 22 '20 at 17:58
  • @user4642212 What is on the right-hand side of the equal sign here: `let x = y++;`? – Scott Marcus May 22 '20 at 20:33
  • @ScottMarcus The word “include” is ambiguous. What I meant by _“Statements include expressions”_ is that `Expression` is a valid stand-alone, non-terminal symbol produced from `Statement` in the formal grammar defined by the spec. Of course, both statements and declarations can include `Expression`s _somewhere_ in them, but an `Expression` _alone_ is not a `Declaration`. `let x = y++;` is a `Declaration` → `LexicalDeclaration` → `LetOrConst` `BindingList` **`;`** → **`let`** `LexicalBinding` **`;`** … etc. This will never resolve to _just_ `Expression` or even _just_ `AssignmentExpression`. – Sebastian Simon May 22 '20 at 21:02
  • @ScottMarcus At any rate, _“Declarations are expressions”_ is wrong: `let x;` is not an expression. _“all declarations include expressions”_ is wrong: the class declaration `class A {}` includes 0 expressions. – Sebastian Simon May 22 '20 at 21:05

1 Answers1

0

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.

Catogram
  • 303
  • 1
  • 12
  • when a `for-loop` is written like `for (;;)` does that mean the for-loop's header is having all `empty-statements` – Kevin May 22 '20 at 15:43
  • 1
    There's no "empty-statement". There is in fact nothing at all. All three parts of the header are optional. – Andreas May 22 '20 at 15:45
  • 1
    Andreas, with further thinking, I agree. I mean you could just call it an empty statement, but that isn't really what is happening since all parameters are optional. – Catogram May 22 '20 at 15:47
  • @Andreas you should read this ["empty-statement"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/Empty) – Kevin May 22 '20 at 15:47
  • @dguis though all the parameter's of a for-loop's header is optional we will have to write `semicolons ;` in the `for-loop's` header which is not optional – Kevin May 22 '20 at 15:58
  • 1
    @Kevin The semicolons simply show that we are not passing in any values for the arguments. They are not the arguments themselves. – Catogram May 22 '20 at 16:59
  • @dguis so you mean both the `semicolons ;` within the header of the `for-loop` `for (; ;)` are not `empty statements` – Kevin May 26 '20 at 14:10
  • @user4642212 thats right but do you know why the ECMAScript specification states that the header of the `for-loop` contains `Expressions` – Kevin May 26 '20 at 14:14
  • @Kevin That’s because the second and third part of a `for` loop head are always expressions (condition, and post-iteration). Only the first one can be a non-expression statement (limited to variable declarations). There are [multiple production rules for a `for` loop](https://tc39.es/ecma262/#sec-for-statement), which make the first part either a `var`, or a `const` or `let` declaration, or any expression. You’re right though, that these aren’t empty statements. All three parts can simply be made _optional_ (due to the `opt` next to each part). The semicolons aren’t optional, though. – Sebastian Simon May 26 '20 at 19:04
  • @user4642212 i guess that the second part of the `for-loop` header is an `Expression statement (THE CONDITION)` [ExpressionStatement](https://tc39.es/ecma262/#prod-ExpressionStatement) because there's a semicolon after the `expression (THE CONDITION)` but the last part in the `for-loop's header (THE POST-ITERATION)` is just an `expression` because there is no semicolon after it – Kevin May 27 '20 at 12:50
  • @Kevin No, the semicolons are part of the `for` statement production itself, _not_ part of the _Expression_ productions within any `for` statement production. Else, you could do `for(let i = 0; i < 9; ; ++i);` which is not valid. – Sebastian Simon May 27 '20 at 12:51