If Else format

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

    If Else format

    On page 90 of my O'Reilly "Javascript The definitive guide" 3rd edition
    there is an example of an If/Else construct: (some text removed)

    If (username != null)
    alert("Hello " + username);
    else {
    username = prompt("What is your name?");
    alert("Hello " + username);
    }


    Should I erase the first ";" from the second line?

    It certainly works better in my tests that way, but I don't know if this
    is an error in the book, a change in the way JavaScript works, or a flaw
    in my browser.

    --
    Steve Swift


  • Erwin Moller

    #2
    Re: If Else format


    Steve Swift schreef:
    On page 90 of my O'Reilly "Javascript The definitive guide" 3rd edition
    there is an example of an If/Else construct: (some text removed)
    >
    If (username != null)
    alert("Hello " + username);
    else {
    username = prompt("What is your name?");
    alert("Hello " + username);
    }
    >
    >
    Should I erase the first ";" from the second line?
    No. Why should you?
    It is the end of a command, and for clarities sake, use a semicolon.
    >
    It certainly works better in my tests that way, but I don't know if this
    is an error in the book, a change in the way JavaScript works, or a flaw
    in my browser.
    How does it work better without the semicolon???

    Erwin Moller



    --
    =============== =============
    Erwin Moller
    Now dropping all postings from googlegroups.
    Why? http://improve-usenet.org/
    =============== =============

    Comment

    • Jorge

      #3
      Re: If Else format

      On Sep 19, 8:17 am, Steve Swift <Steve.J.Sw...@ gmail.comwrote:
      On page 90 of my O'Reilly "Javascript The definitive guide" 3rd edition
      there is an example of an If/Else construct: (some text removed)
      >
      If (username != null)
         alert("Hello " + username);
      else {
         username = prompt("What is your name?");
         alert("Hello " + username);
      >
      }
      >
      Should I erase the first ";" from the second line?
      >
      I'd wrap that alert in curly braces:

      If (username != null) {
      alert("Hello " + username);
      } else {
      username = prompt("What is your name?");
      alert("Hello " + username);
      }

      --
      Jorge.

      Comment

      • SAM

        #4
        Re: If Else format

        Steve Swift a écrit :
        On page 90 of my O'Reilly "Javascript The definitive guide" 3rd edition
        there is an example of an If/Else construct: (some text removed)
        >
        If (username != null)
        no ... : if ( blah )
        (lowercase)
        alert("Hello " + username);
        else {
        username = prompt("What is your name?");
        alert("Hello " + username);
        }
        >
        >
        Should I erase the first ";" from the second line?
        The ';' in end of line is optional in JS


        function hello() {
        if(typeof username == 'undefined' || username == null)
        username = prompt('What is your name?')
        alert('Hello ' + username)
        }

        works as well as :

        function hello() {
        if(typeof username == 'undefined' || username == null) {
        username = prompt('What is your name?');
        }
        alert('Hello ' + username);
        }

        But ... it's much better to code correctly.

        --
        sm

        Comment

        • Steve Swift

          #5
          Re: If Else format

          Jorge wrote:
          I'd wrap that alert in curly braces:
          >
          If (username != null) {
          alert("Hello " + username);
          } else {
          username = prompt("What is your name?");
          alert("Hello " + username);
          }
          Yes, but those curly braces are redundant in the case of the single
          statement above, and in the example. And your ";" is now inside the
          curly braces, so terminating the alert() statement. In my O'Reilly book,
          it seems to be terminating the "If" statement.

          My difficulty is that I don't know if an if/else combination is a single
          statement (in which case the ";" shouldn't have been in the example) or
          two separate statements (in which case I'm wondering why it doesn't work
          in any browser that I've tried).

          The evidence is stacked heavily in favour of an error in the book, but
          stranger things have happened.

          --
          Steve Swift


          Comment

          • Evertjan.

            #6
            Re: If Else format

            Steve Swift wrote on 19 sep 2008 in comp.lang.javas cript:
            Jorge wrote:
            >I'd wrap that alert in curly braces:
            >>
            >If (username != null) {
            > alert("Hello " + username);
            >} else {
            > username = prompt("What is your name?");
            > alert("Hello " + username);
            >}
            >
            Yes, but those curly braces are redundant in the case of the single
            statement above, and in the example. And your ";" is now inside the
            curly braces, so terminating the alert() statement. In my O'Reilly
            book, it seems to be terminating the "If" statement.
            Don't take O'Reilly as a standard.
            Perhaps do not use it at all.
            My difficulty is that I don't know if an if/else combination is a
            single statement (in which case the ";" shouldn't have been in the
            example) or two separate statements (in which case I'm wondering why
            it doesn't work in any browser that I've tried).
            No, the Q is if the

            alert("Hello " + username);

            is a single statement.
            The evidence is stacked heavily in favour of an error in the book, but
            stranger things have happened.
            No, that is not strange, the archive of this NG reports many such errors.



            --
            Evertjan.
            The Netherlands.
            (Please change the x'es to dots in my emailaddress)

            Comment

            • Jeremy J Starcher

              #7
              Re: If Else format

              On Fri, 19 Sep 2008 07:17:31 +0100, Steve Swift wrote:
              On page 90 of my O'Reilly "Javascript The definitive guide" 3rd edition
              there is an example of an If/Else construct: (some text removed)
              >
              If (username != null)
              alert("Hello " + username);
              else {
              username = prompt("What is your name?"); alert("Hello " + username);
              }
              I think that just about everyone in this group will agree with the
              recommendation of: Always use {} in ifs and loops.

              Just solves a lot of headaches.

              if (username != null)
              alert("Hello " + username);
              else
              username = prompt("What is your name?"); alert("Hello " + username);

              is actually the same as:

              if (username != null) {
              alert("Hello " + username);
              } else {
              username = prompt("What is your name?");
              }
              alert("Hello " + username);

              and


              if (username != null)
              alert("Hello " + username)
              ;
              else
              username = prompt("What is your name?"); alert("Hello " + username);

              is the same as:
              if (username != null) {
              alert("Hello " + username);
              }
              ;
              else {
              username = prompt("What is your name?"); alert("Hello " + username);
              }

              Comment

              • Conrad Lender

                #8
                Re: If Else format

                On 2008-09-20 01:45, Jeremy J Starcher wrote:
                I think that just about everyone in this group will agree with the
                recommendation of: Always use {} in ifs and loops.
                No argument there.
                if (username != null)
                alert("Hello " + username)
                ;
                else
                username = prompt("What is your name?"); alert("Hello " + username);
                >
                is the same as:
                if (username != null) {
                alert("Hello " + username);
                }
                ;
                else {
                username = prompt("What is your name?"); alert("Hello " + username);
                }
                .... but argument here.

                Your second example is a syntax error, and thus not the same as the
                first. You can omit the curly braces if - and ONLY if - they contain
                only a single statement or expression. If you do use the braces, you
                cannot insert anything between the closing curly of the if-branch and
                "else" (except for comments and an optional "else if" branch, of
                course), not even an empty statement like ";".

                To answer the OP's question,

                | In my O'Reilly book, it seems to be terminating the "If" statement.

                It does not. This is a special syntactic case in which you're allowed to
                leave the curly braces out, for one branch, but that doesn't mean that's
                the end of the conditional statement.

                When you're in doubt, always use curly braces. Even when there are no
                doubts - you don't really save a lot of space when you write

                if (condition)
                do_something();
                else
                eh_dont();

                instead of

                if (condition) {
                do_something();
                } else {
                eh_dont();
                }

                In my personal opinion, the only time when omitting the curlies is OK is
                when you're doing something like this:

                if (flag_blah) arg += 1;
                if (flag_bloh) arg += 2;
                if (flag_blim) arg += 4;

                Anything more complicated than that - use curlies. And by the way,
                always use the semicolon too, even if it's not technically necessary.


                - Conrad

                Comment

                • Evertjan.

                  #9
                  Re: If Else format

                  Jeremy J Starcher wrote on 20 sep 2008 in comp.lang.javas cript:
                  I think that just about everyone in this group will agree with the
                  recommendation of: Always use {} in ifs and loops.
                  I think not.

                  It depends on the complexity, the need for change by others, and most
                  important the mood of the programmer, "you" or "me".

                  Moreover I think "think that just about everyone in this group" is
                  incorrect, because you did not research it with an open Q,
                  and even then most will not bother to answer, as it is just their own
                  business.

                  Why not simply say you personally recommmend the use of {},
                  in stead of surmizing uncoroborated facts?

                  --
                  Evertjan.
                  The Netherlands.
                  (Please change the x'es to dots in my emailaddress)

                  Comment

                  • Jorge

                    #10
                    Re: If Else format

                    On Sep 20, 3:50 am, Conrad Lender <crlen...@yahoo .comwrote:
                    In my personal opinion, the only time when omitting the curlies
                    is OK is when you're doing something like this:
                    >
                    if (flag_blah) arg += 1;
                    if (flag_bloh) arg += 2;
                    if (flag_blim) arg += 4;
                    >
                    Anything more complicated than that - use curlies.(...)
                    Geekish (?) :

                    javascript:if (1) alert('1'), alert('2');
                    javascript:if (0) alert('1'); alert('2');

                    .... or evil ?

                    Predictable :

                    javascript:if (1) { alert('1'), alert('2'); }
                    javascript:if (0) { alert('1'); alert('2'); }

                    I always use {}.

                    --
                    Jorge.

                    Comment

                    • optimistx

                      #11
                      Ot: opinions vs facts

                      Evertjan. wrote:
                      Jeremy J Starcher wrote on 20 sep 2008 in comp.lang.javas cript:
                      >
                      >I think that just about everyone in this group will agree with the
                      >recommendati on of: Always use {} in ifs and loops.
                      >
                      I think not.
                      >
                      It depends on the complexity, the need for change by others, and most
                      important the mood of the programmer, "you" or "me".
                      >
                      Moreover I think "think that just about everyone in this group" is
                      incorrect, because you did not research it with an open Q,
                      and even then most will not bother to answer, as it is just their own
                      business.
                      >
                      Why not simply say you personally recommmend the use of {},
                      in stead of surmizing uncoroborated facts?
                      You have an interesting opinion.

                      I believe that Jeremy expressed a useful opinion.

                      If I say :

                      1) ' My opinion is A, and I believe the frequency of this
                      opinion is B % among group G'

                      then I transmit more information about my opinions than

                      2) 'My opinion is A'

                      If we allow beliefs and opinions here, why should we not
                      allow expressing beliefs about frequencies, probabilities, numbers?

                      I agree with Jeremy, using common human language,
                      not syntactically strict
                      computer language as in some standards:

                      ' I also believe that almost all in
                      this group think it is good recommendation to use {} in ifs and loops'.

                      If one puts a socially blind syntax checker robot to find errors in
                      the above statement, one gets a long list of error messages.











                      Comment

                      • Jeremy J Starcher

                        #12
                        Re: If Else format

                        > if (username != null)
                        > alert("Hello " + username)
                        > ;
                        > else
                        > username = prompt("What is your name?"); alert("Hello " +
                        > username);
                        >>
                        >is the same as:
                        > if (username != null) {
                        > alert("Hello " + username);
                        > }
                        > ;
                        > else {
                        > username = prompt("What is your name?"); alert("Hello " +
                        > username);
                        > }
                        >
                        ... but argument here.
                        >
                        Your second example is a syntax error, and thus not the same as the
                        first. You can omit the curly braces if - and ONLY if - they contain
                        only a single statement or expression. If you do use the braces, you
                        cannot insert anything between the closing curly of the if-branch and
                        "else" (except for comments and an optional "else if" branch, of
                        course), not even an empty statement like ";".
                        Ouch, mis-understood the specs on that one.

                        Comment

                        • Evertjan.

                          #13
                          Re: Ot: opinions vs facts

                          optimistx wrote on 20 sep 2008 in comp.lang.javas cript:
                          Evertjan. wrote:
                          >Jeremy J Starcher wrote on 20 sep 2008 in comp.lang.javas cript:
                          >>
                          >>I think that just about everyone in this group will agree with the
                          >>recommendatio n of: Always use {} in ifs and loops.
                          >>
                          >I think not.
                          >>
                          >It depends on the complexity, the need for change by others, and most
                          >important the mood of the programmer, "you" or "me".
                          >>
                          >Moreover I think "think that just about everyone in this group" is
                          >incorrect, because you did not research it with an open Q,
                          >and even then most will not bother to answer, as it is just their own
                          >business.
                          >>
                          >Why not simply say you personally recommmend the use of {},
                          >in stead of surmizing uncoroborated facts?
                          >
                          You have an interesting opinion.
                          >
                          I believe that Jeremy expressed a useful opinion.
                          >
                          If I say :
                          >
                          1) ' My opinion is A, and I believe the frequency of this
                          opinion is B % among group G'
                          >
                          then I transmit more information about my opinions than
                          >
                          2) 'My opinion is A'
                          >
                          If we allow beliefs and opinions here, why should we not
                          allow expressing beliefs about frequencies, probabilities, numbers?
                          >
                          I agree with Jeremy, using common human language,
                          not syntactically strict
                          computer language as in some standards:
                          >
                          ' I also believe that almost all in
                          this group think it is good recommendation to use {} in ifs and loops'.
                          You miss the word "all".

                          Then he could be wrong,
                          and he is wrong, in my view.
                          If one puts a socially blind syntax checker robot to find errors in
                          the above statement, one gets a long list of error messages.


                          --
                          Evertjan.
                          The Netherlands.
                          (Please change the x'es to dots in my emailaddress)

                          Comment

                          • Evertjan.

                            #14
                            Re: Ot: opinions vs facts

                            optimistx wrote on 21 sep 2008 in comp.lang.javas cript:
                            >>>>I think that just about everyone in this group will agree with the
                            >>>>recommendat ion of: Always use {} in ifs and loops.
                            ...
                            >>' I also believe that almost all in
                            >>this group think it is good recommendation to use {} in ifs and
                            >>loops'.
                            >>
                            >You miss the word "all".
                            >>
                            >Then he could be wrong,
                            >and he is wrong, in my view.
                            >
                            all? Where?
                            .... good recommendation to use {} in ALL ifs and loops' ...

                            I do not agree with that.
                            'about everyone' might not be correct official English, but I
                            as a foreigner understand that meaning 'almost all'. And how many
                            percent of all that might be? at least 51%? at least 75 %? at least 90%?
                            You were on the wrong track.

                            --
                            Evertjan.
                            The Netherlands.
                            (Please change the x'es to dots in my emailaddress)

                            Comment

                            • optimistx

                              #15
                              Re: Ot: opinions vs facts

                              Evertjan. wrote:
                              optimistx wrote on 21 sep 2008 in comp.lang.javas cript:
                              ....
                              ... good recommendation to use {} in ALL ifs and loops' ...
                              >
                              I do not agree with that.
                              >
                              >'about everyone' might not be correct official English, but I
                              >as a foreigner understand that meaning 'almost all'. And how many
                              >percent of all that might be? at least 51%? at least 75 %? at least
                              >90%?
                              >
                              You were on the wrong track.
                              Yes, I was, thanks for the correction.

                              If we consider everyday language expression

                              '...good recommendation to use {} in all ifs and loops'

                              we may ask :' if it is not a good recommendation, what is it? Bad?'.

                              http://jslint.com recommends:

                              --- start of quote ---
                              '
                              Required Blocks
                              JSLint expects that if and for statements will be made with blocks {that is,
                              with statements enclosed in braces}.

                              JavaScript allows an if to be written like this:

                              if (condition)
                              statement;That form is known to contribute to mistakes in projects where
                              many programmers are working on the same code. That is why JSLint expects
                              the use of a block:

                              if (condition) {
                              statements;
                              }Experience shows that this form is more resilient.'

                              ---end of quote ---

                              and I believe that this IS a good recommendation and I believe most other
                              writers in this newsgroup agree with that :).






                              Comment

                              Working...