Yes/No

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

    #1

    Yes/No

    Are the two statements same:
    a<=20?b=30:c=30 ;
    (a<=20)?b=30:c= 30;

  • Ian Malone

    #2
    Re: Yes/No

    Meenu wrote:[color=blue]
    > Are the two statements same:
    > a<=20?b=30:c=30 ;
    > (a<=20)?b=30:c= 30;
    >[/color]

    Google "C++ operator precedence"


    (yes, unless you have some funny #define going round)

    --
    imalone

    Comment

    • Meenu

      #3
      Re: Yes/No



      Ian Malone wrote:[color=blue]
      > Meenu wrote:[color=green]
      > > Are the two statements same:
      > > a<=20?b=30:c=30 ;
      > > (a<=20)?b=30:c= 30;
      > >[/color]
      >
      > Google "C++ operator precedence"
      >
      >
      > (yes, unless you have some funny #define going round)
      >
      > --
      > imalone[/color]

      Even I thought so, but the answer mentioned is no, and I can't figure
      out a reason

      Comment

      • Ian Malone

        #4
        Re: Yes/No

        Ian Malone wrote:
        [color=blue]
        >
        > Google "C++ operator precedence"
        >[/color]

        Oops, wrong ng. Obviously that says 'c operator precedence'

        --
        imalone

        Comment

        • Meenu

          #5
          Re: Yes/No



          Ian Malone wrote:[color=blue]
          > Ian Malone wrote:
          >[color=green]
          > >
          > > Google "C++ operator precedence"
          > >[/color]
          >
          > Oops, wrong ng. Obviously that says 'c operator precedence'
          >
          > --
          > imalone[/color]

          I didn't get it...:(
          pls elaborate

          Comment

          • Ian Malone

            #6
            Re: Yes/No

            Meenu wrote:[color=blue]
            >
            > Ian Malone wrote:
            >[color=green]
            >>Ian Malone wrote:
            >>
            >>[color=darkred]
            >>>Google "C++ operator precedence"
            >>>[/color]
            >>
            >>Oops, wrong ng. Obviously that says 'c operator precedence'
            >>[/color][/color]
            [color=blue]
            >
            > I didn't get it...:(
            > pls elaborate
            >[/color]

            Thought I was reading comp.lang.c++, not comp.lang.c.

            Incidentally, since the ternary operator is meant to
            return a value, the assignments need to be parenthesised,
            since they have a lower priority (they are statements to
            be evaluated). You need
            a<=20?(b=30):(c =30);

            --
            imalone

            Comment

            • Simon Morgan

              #7
              Re: Yes/No

              On Fri, 22 Jul 2005 03:39:33 -0700, Meenu wrote:
              [color=blue]
              > Are the two statements same:
              > a<=20?b=30:c=30 ;
              > (a<=20)?b=30:c= 30;[/color]

              How's the homework going? Obviously not very well if you still haven't
              grasped basic operator precedence. May I suggest reading a book or
              actually attending class?

              Comment

              • Meenu

                #8
                Re: Yes/No



                Simon Morgan wrote:[color=blue]
                > On Fri, 22 Jul 2005 03:39:33 -0700, Meenu wrote:
                >[color=green]
                > > Are the two statements same:
                > > a<=20?b=30:c=30 ;
                > > (a<=20)?b=30:c= 30;[/color]
                >
                > How's the homework going? Obviously not very well if you still haven't
                > grasped basic operator precedence. May I suggest reading a book or
                > actually attending class?[/color]

                Well thanks,
                my confusion was that since parentheses only make the condition part
                easier to see, other than that, i don't feel there would be any change
                in the output.

                Comment

                • Dik T. Winter

                  #9
                  Re: Yes/No

                  In article <1122028773.687 405.231770@g14g 2000cwa.googleg roups.com> "Meenu" <meenu_kg@yahoo .com> writes:[color=blue]
                  > Are the two statements same:
                  > a<=20?b=30:c=30 ;
                  > (a<=20)?b=30:c= 30;[/color]

                  The same in what way? Both should give an error message from the compiler.
                  --
                  dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
                  home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/

                  Comment

                  • CBFalconer

                    #10
                    Re: Yes/No

                    Meenu wrote:[color=blue]
                    >
                    > Are the two statements same:
                    > a<=20?b=30:c=30 ;
                    > (a<=20)?b=30:c= 30;[/color]

                    I wouldn't even bother to read them. They are infested with blank
                    elidation.

                    --
                    Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                    Available for consulting/temporary embedded and systems.
                    <http://cbfalconer.home .att.net> USE worldnet address!


                    Comment

                    • lawrence.jones@ugs.com

                      #11
                      Re: Yes/No

                      Ian Malone <ibm21@cam.ac.u k> wrote:[color=blue]
                      >
                      > Incidentally, since the ternary operator is meant to
                      > return a value, the assignments need to be parenthesised,
                      > since they have a lower priority (they are statements to
                      > be evaluated). You need
                      > a<=20?(b=30):(c =30);[/color]

                      The ? and : act like parentheses, so you only need to parenthesize the
                      final assignment:

                      a <= 20 ? b = 30 : (c = 30);

                      -Larry Jones

                      Mom must've put my cape in the wrong drawer. -- Calvin

                      Comment

                      • Eric Sosman

                        #12
                        [OT] Re: Yes/No

                        CBFalconer wrote:
                        [color=blue]
                        > Meenu wrote:
                        >[color=green]
                        >>Are the two statements same:
                        >>a<=20?b=30:c= 30;
                        >>(a<=20)?b=30: c=30;[/color]
                        >
                        >
                        > I wouldn't even bother to read them. They are infested with blank
                        > elidation.[/color]

                        The word you seek is "elision." As in "The Elision Fields,"
                        where one forgets one's past lives -- Egad! I'm turning Buddhist.

                        (Interesting thought: How can something be "infested with"
                        an absence? Maybe I'd understand better if I were infested
                        with an absinthe ...)

                        --
                        Eric Sosman
                        esosman@acm-dot-org.invalid

                        Comment

                        • CBFalconer

                          #13
                          Re: [OT] Re: Yes/No

                          Eric Sosman wrote:[color=blue]
                          > CBFalconer wrote:[color=green]
                          >> Meenu wrote:
                          >>[color=darkred]
                          >>> Are the two statements same:
                          >>> a<=20?b=30:c=30 ;
                          >>> (a<=20)?b=30:c= 30;[/color]
                          >>
                          >> I wouldn't even bother to read them. They are infested with blank
                          >> elidation.[/color]
                          >
                          > The word you seek is "elision." As in "The Elision Fields,"
                          > where one forgets one's past lives -- Egad! I'm turning Buddhist.
                          >
                          > (Interesting thought: How can something be "infested with"
                          > an absence? Maybe I'd understand better if I were infested
                          > with an absinthe ...)[/color]

                          Yeah, it didn't look right. Would you settle for Pernod?

                          --
                          Chuck F (cbfalconer@yah oo.com) (cbfalconer@wor ldnet.att.net)
                          Available for consulting/temporary embedded and systems.
                          <http://cbfalconer.home .att.net> USE worldnet address!


                          Comment

                          • junky_fellow@yahoo.co.in

                            #14
                            Re: Yes/No



                            Meenu wrote:[color=blue]
                            > Are the two statements same:
                            > a<=20?b=30:c=30 ;
                            > (a<=20)?b=30:c= 30;[/color]

                            No. In fact both will generate compilation error.
                            Since precedence of conditional operator is higher than
                            assignement operator, the following statement
                            a<=20?b=30:c=30 ; will be interpreted like

                            (a<=20?b=30:c)= 30;
                            LHS is not an lvalue, so the error.
                            Same is the reason for second statement.
                            Now, if you enclose "c=30" in parenthesis, it would
                            be interpreted as:
                            (a<=20?b=30:(c= 30)); which would complile OK.

                            Comment

                            • Keith Thompson

                              #15
                              Re: Yes/No

                              junky_fellow@ya hoo.co.in writes:[color=blue]
                              > Meenu wrote:[color=green]
                              >> Are the two statements same:
                              >> a<=20?b=30:c=30 ;
                              >> (a<=20)?b=30:c= 30;[/color]
                              >
                              > No. In fact both will generate compilation error.[/color]

                              Doesn't that make them equivalent?

                              --
                              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                              San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                              We must do something. This is something. Therefore, we must do this.

                              Comment

                              Working...