expression evaluation : help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • manan.kathuria@gmail.com

    #1

    expression evaluation : help!

    hi all ,
    the expression in question is

    ++i&&++j||++k

    most sources say that since the result of the || operation is decided
    by the LHS itself , the right side is not computed

    my point of thinking is that since the unary operator has higher
    precedence than || , it will be evaluated before || in any
    case........... where am im going wrong

    thanks in anticipation

    Manan

  • Joona I Palaste

    #2
    Re: expression evaluation : help!

    manan.kathuria@ gmail.com scribbled the following:[color=blue]
    > hi all ,
    > the expression in question is[/color]
    [color=blue]
    > ++i&&++j||++k[/color]
    [color=blue]
    > most sources say that since the result of the || operation is decided
    > by the LHS itself , the right side is not computed[/color]
    [color=blue]
    > my point of thinking is that since the unary operator has higher
    > precedence than || , it will be evaluated before || in any
    > case........... where am im going wrong[/color]

    Where you're going wrong is thinking precedence and order of evaluation
    are the same thing. They're nothing of the sort. Precedence dictates how
    a string of C tokens is compiled into an expression. Order of evaluation
    dictates how that expression is evaluated at run-time.

    --
    /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "C++. C++ run. Run, ++, run."
    - JIPsoft

    Comment

    • Michael Mair

      #3
      Re: expression evaluation : help!

      manan.kathuria@ gmail.com wrote:[color=blue]
      > hi all ,
      > the expression in question is
      >
      > ++i&&++j||++k
      >
      > most sources say that since the result of the || operation is decided
      > by the LHS itself , the right side is not computed
      >
      > my point of thinking is that since the unary operator has higher
      > precedence than || , it will be evaluated before || in any
      > case........... where am im going wrong[/color]

      See FAQ 3.5 and 3.8, best read the whole section 3:


      Note: The ASCII text version posted here by Steve Summit is
      more complete/up to date than the above HTML version but can
      be obtained following the links from



      Cheers
      Michael
      --
      E-Mail: Mine is an /at/ gmx /dot/ de address.

      Comment

      • Joona I Palaste

        #4
        Re: expression evaluation : help!

        Michael Mair <Michael.Mair@i nvalid.invalid> scribbled the following:[color=blue]
        > manan.kathuria@ gmail.com wrote:[color=green]
        >> hi all ,
        >> the expression in question is
        >>
        >> ++i&&++j||++k
        >>
        >> most sources say that since the result of the || operation is decided
        >> by the LHS itself , the right side is not computed
        >>
        >> my point of thinking is that since the unary operator has higher
        >> precedence than || , it will be evaluated before || in any
        >> case........... where am im going wrong[/color][/color]
        [color=blue]
        > See FAQ 3.5 and 3.8, best read the whole section 3:
        > http://www.eskimo.com/~scs/C-faq/s3.html[/color]
        [color=blue]
        > Note: The ASCII text version posted here by Steve Summit is
        > more complete/up to date than the above HTML version but can
        > be obtained following the links from
        > http://www.eskimo.com/~scs/C-faq/top.html[/color]

        ITYM also see FAQ 3.4, which answers the OP's question pretty much
        directly.

        --
        /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
        \-------------------------------------------------------- rules! --------/
        "I wish someone we knew would die so we could leave them flowers."
        - A 6-year-old girl, upon seeing flowers in a cemetery

        Comment

        • Clark S. Cox III

          #5
          Re: expression evaluation : help!

          On 2005-02-22 13:55:49 -0500, manan.kathuria@ gmail.com said:
          [color=blue]
          > hi all ,
          > the expression in question is
          >
          > ++i&&++j||++k
          >
          > most sources say that since the result of the || operation is decided
          > by the LHS itself , the right side is not computed[/color]

          This is true. If the left-hand side of || is true, then the right-hand
          side will never be evaluated. So, in your above expression, if (++i)
          evaluates to a non-zero value, and (++j) does as well, then k will
          never be incremented.
          [color=blue]
          > my point of thinking is that since the unary operator has higher
          > precedence than || , it will be evaluated before || in any
          > case........... where am im going wrong[/color]

          The point that you're missing is that precedence has nothing to do with
          order of evaluation.

          --
          Clark S. Cox, III
          clarkcox3@gmail .com

          Comment

          • Michael Mair

            #6
            Re: expression evaluation : help!

            Joona I Palaste wrote:[color=blue]
            > Michael Mair <Michael.Mair@i nvalid.invalid> scribbled the following:
            >[color=green]
            >>manan.kathuri a@gmail.com wrote:
            >>[color=darkred]
            >>>hi all ,
            >>>the expression in question is
            >>>
            >>>++i&&++j||++ k
            >>>
            >>>most sources say that since the result of the || operation is decided
            >>>by the LHS itself , the right side is not computed
            >>>
            >>>my point of thinking is that since the unary operator has higher
            >>>precedence than || , it will be evaluated before || in any
            >>>case........ ...where am im going wrong[/color][/color]
            >
            >[color=green]
            >>See FAQ 3.5 and 3.8, best read the whole section 3:
            >> http://www.eskimo.com/~scs/C-faq/s3.html[/color]
            >
            >[color=green]
            >>Note: The ASCII text version posted here by Steve Summit is
            >>more complete/up to date than the above HTML version but can
            >>be obtained following the links from
            >> http://www.eskimo.com/~scs/C-faq/top.html[/color]
            >
            >
            > ITYM also see FAQ 3.4, which answers the OP's question pretty much
            > directly.[/color]

            No, I plainly oversaw it when I was quickly scanning for special
            questions to point out -- some people are scared by
            "read the whole section" ;-)


            Cheers
            Michael
            --
            E-Mail: Mine is an /at/ gmx /dot/ de address.

            Comment

            • Joona I Palaste

              #7
              Re: expression evaluation : help!

              Michael Mair <Michael.Mair@i nvalid.invalid> scribbled the following:[color=blue]
              > Joona I Palaste wrote:[color=green]
              >> Michael Mair <Michael.Mair@i nvalid.invalid> scribbled the following:[color=darkred]
              >>>manan.kathur ia@gmail.com wrote:
              >>>>hi all ,
              >>>>the expression in question is
              >>>>
              >>>>++i&&++j||+ +k
              >>>>
              >>>>most sources say that since the result of the || operation is decided
              >>>>by the LHS itself , the right side is not computed
              >>>>
              >>>>my point of thinking is that since the unary operator has higher
              >>>>precedenc e than || , it will be evaluated before || in any
              >>>>case....... ....where am im going wrong[/color]
              >>[color=darkred]
              >>>See FAQ 3.5 and 3.8, best read the whole section 3:
              >>> http://www.eskimo.com/~scs/C-faq/s3.html[/color]
              >>[color=darkred]
              >>>Note: The ASCII text version posted here by Steve Summit is
              >>>more complete/up to date than the above HTML version but can
              >>>be obtained following the links from
              >>> http://www.eskimo.com/~scs/C-faq/top.html[/color]
              >>
              >> ITYM also see FAQ 3.4, which answers the OP's question pretty much
              >> directly.[/color][/color]
              [color=blue]
              > No, I plainly oversaw it when I was quickly scanning for special
              > questions to point out -- some people are scared by
              > "read the whole section" ;-)[/color]

              Does your reply "no" to my "ITYM" mean "no, that's just plain wrong" or
              "no, I didn't think of that"?

              --
              /-- Joona Palaste (palaste@cc.hel sinki.fi) ------------- Finland --------\
              \-------------------------------------------------------- rules! --------/
              "I am looking for myself. Have you seen me somewhere?"
              - Anon

              Comment

              • Michael Mair

                #8
                Re: expression evaluation : help!

                Joona I Palaste wrote:[color=blue]
                > Michael Mair <Michael.Mair@i nvalid.invalid> scribbled the following:
                >[color=green]
                >>Joona I Palaste wrote:
                >>[color=darkred]
                >>>Michael Mair <Michael.Mair@i nvalid.invalid> scribbled the following:
                >>>
                >>>>manan.kathu ria@gmail.com wrote:
                >>>>
                >>>>>hi all ,
                >>>>>the expression in question is
                >>>>>
                >>>>>++i&&++j|| ++k
                >>>>>
                >>>>>most sources say that since the result of the || operation is decided
                >>>>>by the LHS itself , the right side is not computed
                >>>>>
                >>>>>my point of thinking is that since the unary operator has higher
                >>>>>preceden ce than || , it will be evaluated before || in any
                >>>>>case...... .....where am im going wrong
                >>>
                >>>>See FAQ 3.5 and 3.8, best read the whole section 3:
                >>>> http://www.eskimo.com/~scs/C-faq/s3.html
                >>>
                >>>>Note: The ASCII text version posted here by Steve Summit is
                >>>>more complete/up to date than the above HTML version but can
                >>>>be obtained following the links from
                >>>> http://www.eskimo.com/~scs/C-faq/top.html
                >>>
                >>>ITYM also see FAQ 3.4, which answers the OP's question pretty much
                >>>directly.[/color][/color]
                >[color=green]
                >>No, I plainly oversaw it when I was quickly scanning for special
                >>questions to point out -- some people are scared by
                >>"read the whole section" ;-)[/color]
                >
                > Does your reply "no" to my "ITYM" mean "no, that's just plain wrong" or
                > "no, I didn't think of that"?[/color]

                The latter :-)


                Cheers
                Michael
                --
                E-Mail: Mine is an /at/ gmx /dot/ de address.

                Comment

                • manan.kathuria@gmail.com

                  #9
                  Re: expression evaluation : help!

                  thanks a lot everyone ....cheers
                  manan

                  Comment

                  Working...