";" after if statement??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JS

    ";" after if statement??

    In this sample code:

    if(n==0&&args>1 ){
    for(i=num;args> i+1;i++){
    s[i].length = 0;
    opt = document.create Element('OPTION ');
    s[i].appendChild(op t);
    opt.value = "";
    opt.text = "\74-- Vælg --";
    }
    return true
    };

    the if statement is ended by a ";" but I have seen other if statments that
    do not end with that. Why is if statement sometimes ended by a ";".?


  • Deniz Adrian

    #2
    Re: ";&quot ; after if statement??

    JS wrote:[color=blue]
    > In this sample code:
    >
    > if(n==0&&args>1 ){
    > for(i=num;args> i+1;i++){
    > s[i].length = 0;
    > opt = document.create Element('OPTION ');
    > s[i].appendChild(op t);
    > opt.value = "";
    > opt.text = "\74-- Vælg --";
    > }
    > return true
    > };
    >
    > the if statement is ended by a ";" but I have seen other if statments that
    > do not end with that. Why is if statement sometimes ended by a ";".?
    >
    >[/color]

    I guess, the semicolon should be after "return true" and not after the
    closing bracket.

    best regards
    Deniz

    Comment

    • Richard Cornford

      #3
      Re: ";&quot ; after if statement??

      JS wrote:[color=blue]
      > In this sample code:
      >
      > if(n==0&&args>1 ){
      > for(i=num;args> i+1;i++){
      > s[i].length = 0;
      > opt = document.create Element('OPTION ');
      > s[i].appendChild(op t);
      > opt.value = "";
      > opt.text = "\74-- Vælg --";
      > }
      > return true
      > };
      >
      > the if statement is ended by a ";"[/color]

      No it isn't. The semicolon, following the closing brace that represents
      the end of the Block statement that is the conditionally executed
      statement of the IfStatement, is an EmptyStatment. Harmless but
      unnecessary.
      [color=blue]
      > but I have seen other if statments
      > that do not end with that.[/color]

      The IfStatement production does not require a terminating semicolon. If
      the Statement ending the production - if ( Expression) Statement - is
      not a Block statement then that statement may need to be terminated with
      a semicolon, giving the impression that the IfStatment ends with a
      semicolon.

      ECMAScript performs 'automatic semicolon insertion' so under some
      circumstances semicolons that are required by the production rules may
      be omitted as the interpreter will insert them itself. See ECMA 262 3rd
      edition section 7.9.
      [color=blue]
      > Why is if statement sometimes ended by a ";".?[/color]

      The existence of the EmptyStatment - ; - (which is harmless in most
      contexts) and automatic semicolon insertion allows script authors to get
      away with having little understanding of when and where semicolons are
      necessary in javascript. That lack of understanding can be observed when
      looking at example scripts.

      Richard.


      Comment

      • Michael Winter

        #4
        Re: ";&quot ; after if statement??

        On 27/05/2005 08:56, Richard Cornford wrote:

        [snip]
        [color=blue]
        > The existence of the EmptyStatment - ; - (which is harmless in most
        > contexts) and automatic semicolon insertion allows script authors to get
        > away with having little understanding of when and where semicolons are
        > necessary in javascript. [...][/color]

        I'd disagree with the former, and agree with the latter.

        Empty statements are a part of other languages, including compiled ones,
        which have no qualms over enforcing strict adherence to the grammar. I
        can't think of enough examples to decide if empty statements are ever
        necessary, but they can be used, for example, in iterative statements
        where the condition also performs an action that renders the loop body
        redundant:

        while( condition );

        However, this could be equally represented with:

        while( condition ) {}

        as the StatementList within a Block is optional. This is true in C (and
        C++), and Java (the grammar summary for the latter is misprinted).

        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        • Richard Cornford

          #5
          Re: ";&quot ; after if statement??

          Michael Winter wrote:[color=blue]
          > Richard Cornford wrote:[/color]
          <snip>[color=blue][color=green]
          >> The existence of the EmptyStatment - ; - (which is
          >> harmless in most contexts) and automatic semicolon
          >> insertion allows script authors to get away with
          >> having little understanding of when and where
          >> semicolons are necessary in javascript. [...][/color]
          >
          > I'd disagree with the former, and agree with the latter.
          >
          > Empty statements are a part of other languages, including
          > compiled ones, which have no qualms over enforcing strict
          > adherence to the grammar.[/color]
          <snip>[color=blue]
          > while( condition );[/color]
          <snip>

          I have nothing against empty statments, and would prefer using one in an
          IterationStatem ent where all the work was done in the control expression
          (rather than an empty block).

          But having empty statements allows them to be inserted where semicolons
          would not otherwise be necessary (you would get some sort of error if
          they did not exist), and that is true of compiled languages as well,
          except in compiled languages you don't get away with omitting the
          necessary semicolons so you end up learning where they need to be very
          quickly.

          The OP's question relates to an unnecessary EmptyStatement rather than
          the missing semicolon at the end of the ReturnStatement (which is fixed
          by automatic semicolon insertion). An example, seeing:-

          var x = function(){
          ... // function body
          };

          - which is correct because the assignment expression becomes an
          ExpressionState ment and should be semicolon terminated, someone might
          infer:-

          function x(){
          ... // function body
          };

          - where the semicolon is an EmptyStatement independent of the function
          declaration. But getting away with that may serve to blur the
          distinction between a function declaration and a function expression as
          a side effect.

          Richard.


          Comment

          • Richard Cornford

            #6
            Re: &quot;;&quot ; after if statement??

            Richard Cornford wrote:
            <snip>[color=blue]
            > var x = function(){
            > ... // function body
            > };
            >
            > - which is correct because the assignment expression becomes
            > an ExpressionState ment and should be semicolon terminated, ...[/color]
            <snip>

            Actually the assignment expression becomes part of a VariableStateme nt,
            which is also semicolon terminated. I probably should have left the -
            var - keyword out.

            Richard.


            Comment

            • John G Harris

              #7
              Re: &quot;;&quot ; after if statement??

              In article <d76h6u$ocs$1@n ews.net.uni-c.dk>, JS <dsa.?@asdf.com .invalid>
              writes[color=blue]
              >In this sample code:
              >
              > if(n==0&&args>1 ){
              > for(i=num;args> i+1;i++){
              > s[i].length = 0;
              > opt = document.create Element('OPTION ');
              > s[i].appendChild(op t);
              > opt.value = "";
              > opt.text = "\74-- Vælg --";
              > }
              > return true
              > };
              >
              >the if statement is ended by a ";" but I have seen other if statments that
              >do not end with that. Why is if statement sometimes ended by a ";".?[/color]

              It might help to understand other people's answers if the code is laid
              out to show how the javascript parser will see it :

              if (n==0 && args>1)
              {
              for (i=num; args>i+1; i++)
              {
              s[i].length = 0;
              opt = document.create Element('OPTION ');
              s[i].appendChild(op t);
              opt.value = "";
              opt.text = "\74-- Vælg --";
              }
              return true
              }

              ;

              Each extra level of indenting marks a statement or statements nested
              inside another statement. (The parser will also automatically add a
              semicolon after 'true').

              John
              --
              John Harris

              Comment

              Working...