0

function FirstFactorial(num) {

var rval=1;
for (var i = 2; i <= num; i++)

 rval *= i;  

return rval;
}

FirstFactorial(4);

Why does the above function--which computes the factorial of a given number--need no "do" brackets -> ie, "{ }", placed after its For statement in order for it to work? I thought after for(...) one always put "{...code to execute here...}".

However, when I tried adding the "{}"s to that For statement shown up above, the code returned the wrong answer of "2" instead of "24" as the result of "4*2*3*1" aka "FirstFactorial(4)". I'm puzzled. Please somebody help me understand when to include do brackets in functions, and when they're unnecessary?

Thank you in advance to whomever may help...

  • 4
    One expression after the for? No braces. Multiple expressions? Braces. But if you always put them you don't have to worry about not putting them. – elclanrs Mar 31 '14 at 03:50
  • 1
    The curlybraces are optional, but without them the loops context stops at the next semicolon. – adeneo Mar 31 '14 at 03:51
  • Thank you both for the speedy and helpful answer. My additional question, only if you could: Why does the same above function NOT return the correct result ONLY when the curly braces are applied to its For statement? Calling them "optional" seems to me that they should work either way; used or not. However, I returned "2" when using braces, and got my "24" (correct answer) running it without braces, above. – user3479657 Mar 31 '14 at 03:57
  • Sounds like you included the `return` in the `{}` brackets. That would make it return on the first loop, with rval=2 from multiplying 1*2. – Paul Mar 31 '14 at 03:57
  • 1
    @adeneo—not forgetting automatic semicolon insertion will add them wherever it can, so the first statement may end before the next semicolon in the source. – RobG Mar 31 '14 at 03:57
  • Pasting your code into http://www.jslint.com/ and then reading what Douglas Crockford, who wrote jslint, has to say in papers like http://javascript.crockford.com/code.html will increase your understanding of JavaScript. Once you start down that path you'll find yourself using tools like http://eslint.org/ before long. – Jason Aller Mar 31 '14 at 04:20
  • I strongly advise reading Jon Skeet's answer to [Is it ok if I omit curly braces in Java?](http://stackoverflow.com/questions/8020228/is-it-ok-if-i-omit-curly-braces-in-java/8020255#8020255) Even though it's in reference to Java, it applies just as well. – Adi Inbar Mar 31 '14 at 06:45

2 Answers2

0

You've been answered in comments, but for completeness…

The specification of a for statement is:

for ( ExpressionNoInopt ; Expressionopt ; Expressionopt) Statement

The bit at the end, the Statement, can be a single statement or a list of statements in a block. So for:

for (...) {
  statement_0;
  statement_1;
  statement_2;
}

all of the statements (0 to 2 inclusive) will be executed on each iteration. But without the braces (i.e. without the block) only the first statement (*statement_0* in the following) is executed.

for (...)
  statement_0;
statement_1;
statement_2;

Note that if you ommit semicolons and braces, then the compliler will decide where the semicolons go and hence statements end based on the rules for automatic semicolon insertion.

RobG
  • 142,382
  • 31
  • 172
  • 209
0

As in many 'curly brace' languages, the body of a compound statement follows the control statement, i.e. if (i<n) stmt, or for (i=0; i>n; i++) stmt. If you want more than one statement in the body of your loop, you do it by enclosing the multiple statements within curly braces, so that the collection of statements is treated as one statement. If you leave them out, it'll just take the next statement as the body of your loop.

Unlike Python or Coffeescript, the Javascript interpreter doesn't pay any attention to how it's indented, or even what line it's on (blank lines are ignored) so in your example, the line rval *= i; is the body of your loop. If you put curly braces around just that, it should work the same (it does for me).

Opinions differ, but I find it always safest to put the curly braces in, it tends to avoid problems in the long run (like adding a statement with the same indentation that looks like it's in the body of the loop but isn't). The only exception is when it makes sense to do everything on one line, e.g.:

if (x<0) return;
OldGeeksGuide
  • 2,888
  • 13
  • 23