Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. syntax question

syntax question

Scheduled Pinned Locked Moved C / C++ / MFC
question
13 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lior Shoval
    wrote on last edited by
    #1

    Hi. Can anyone explain to me once and for all why do i see people write something like: if (0==myVar) { ... } instead of: if (myVar==0) { ... } what difference does it make ?

    C 1 Reply Last reply
    0
    • L Lior Shoval

      Hi. Can anyone explain to me once and for all why do i see people write something like: if (0==myVar) { ... } instead of: if (myVar==0) { ... } what difference does it make ?

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Simple. Your compiler will tell you if you type if (0 = myVar) whereas if (myVar = 0 ) will compile and will always set myVar to 0 and evaluate to true. Christian NO MATTER HOW MUCH BIG IS THE WORD SIZE ,THE DATA MUCT BE TRANSPORTED INTO THE CPU. - Vinod Sharma Anonymous wrote: OK. I read a c++ book. Or...a bit of it anyway. I'm sick of that evil looking console window. I think you are a good candidate for Visual Basic. - Nemanja Trifunovic

      J L H 3 Replies Last reply
      0
      • C Christian Graus

        Simple. Your compiler will tell you if you type if (0 = myVar) whereas if (myVar = 0 ) will compile and will always set myVar to 0 and evaluate to true. Christian NO MATTER HOW MUCH BIG IS THE WORD SIZE ,THE DATA MUCT BE TRANSPORTED INTO THE CPU. - Vinod Sharma Anonymous wrote: OK. I read a c++ book. Or...a bit of it anyway. I'm sick of that evil looking console window. I think you are a good candidate for Visual Basic. - Nemanja Trifunovic

        J Offline
        J Offline
        Johann Gerell
        wrote on last edited by
        #3

        Christian Graus wrote: if (myVar = 0 ) will compile and will always set myVar to 0 and evaluate to true. A typo, I guess Christian... :) The value of the expression "A = B" is the value of A after the assignment, i.e. the value of B. Thus, "myVar = 0" evaluates to 0 and false. -- Human beings, who are almost unique in having the ability
        to learn from the experience of others, are also remarkable
        for their apparent disinclination to do so. (Douglas Adams)

        L 1 Reply Last reply
        0
        • J Johann Gerell

          Christian Graus wrote: if (myVar = 0 ) will compile and will always set myVar to 0 and evaluate to true. A typo, I guess Christian... :) The value of the expression "A = B" is the value of A after the assignment, i.e. the value of B. Thus, "myVar = 0" evaluates to 0 and false. -- Human beings, who are almost unique in having the ability
          to learn from the experience of others, are also remarkable
          for their apparent disinclination to do so. (Douglas Adams)

          L Offline
          L Offline
          Lior Shoval
          wrote on last edited by
          #4

          both of you got it wrong... i asked what is the difference between: if (a==0) { } and if (0==a) { } and not between: if (a=0) { } and if (0=a) { } (notice the extra = sign which makes a big difference)

          J 1 Reply Last reply
          0
          • C Christian Graus

            Simple. Your compiler will tell you if you type if (0 = myVar) whereas if (myVar = 0 ) will compile and will always set myVar to 0 and evaluate to true. Christian NO MATTER HOW MUCH BIG IS THE WORD SIZE ,THE DATA MUCT BE TRANSPORTED INTO THE CPU. - Vinod Sharma Anonymous wrote: OK. I read a c++ book. Or...a bit of it anyway. I'm sick of that evil looking console window. I think you are a good candidate for Visual Basic. - Nemanja Trifunovic

            L Offline
            L Offline
            Lior Shoval
            wrote on last edited by
            #5

            checkout my reply to Johann and try to re-answer. Thanks.

            G 1 Reply Last reply
            0
            • L Lior Shoval

              checkout my reply to Johann and try to re-answer. Thanks.

              G Offline
              G Offline
              Gary R Wheeler
              wrote on last edited by
              #6

              They did answer. If you get in the habit of always typing the constant first, the compiler will emit an error message when you mistakenly type one equals ('=') instead of two ('=='). Other than that, the only difference between (a == b) and (b == a) is the order of evaluation.


              Software Zen: delete this;

              L L 2 Replies Last reply
              0
              • G Gary R Wheeler

                They did answer. If you get in the habit of always typing the constant first, the compiler will emit an error message when you mistakenly type one equals ('=') instead of two ('=='). Other than that, the only difference between (a == b) and (b == a) is the order of evaluation.


                Software Zen: delete this;

                L Offline
                L Offline
                Lior Shoval
                wrote on last edited by
                #7

                Thanks. I was under the impression that it had somthing to do with compiler or preprocessor optimizations...

                1 Reply Last reply
                0
                • G Gary R Wheeler

                  They did answer. If you get in the habit of always typing the constant first, the compiler will emit an error message when you mistakenly type one equals ('=') instead of two ('=='). Other than that, the only difference between (a == b) and (b == a) is the order of evaluation.


                  Software Zen: delete this;

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  That means, that using the first method is better to catch typos like "=" that act like assignments? regards

                  G 1 Reply Last reply
                  0
                  • L Lior Shoval

                    both of you got it wrong... i asked what is the difference between: if (a==0) { } and if (0==a) { } and not between: if (a=0) { } and if (0=a) { } (notice the extra = sign which makes a big difference)

                    J Offline
                    J Offline
                    Johann Gerell
                    wrote on last edited by
                    #9

                    Lior Shoval wrote: both of you got it wrong... i asked what is the difference between: if (a==0) { } and if (0==a) { } Christian did answer that one (although he rationalized a couple of '"''s off :)). I merely made it clearer what a = 0 evaluates to. -- Human beings, who are almost unique in having the ability
                    to learn from the experience of others, are also remarkable
                    for their apparent disinclination to do so. (Douglas Adams)

                    1 Reply Last reply
                    0
                    • C Christian Graus

                      Simple. Your compiler will tell you if you type if (0 = myVar) whereas if (myVar = 0 ) will compile and will always set myVar to 0 and evaluate to true. Christian NO MATTER HOW MUCH BIG IS THE WORD SIZE ,THE DATA MUCT BE TRANSPORTED INTO THE CPU. - Vinod Sharma Anonymous wrote: OK. I read a c++ book. Or...a bit of it anyway. I'm sick of that evil looking console window. I think you are a good candidate for Visual Basic. - Nemanja Trifunovic

                      H Offline
                      H Offline
                      Hans Dietrich
                      wrote on last edited by
                      #10

                      Unless, of course, you use warning level 4, which will give the compiler warning "warning C4706: assignment within conditional expression" if you try to do if (myVar = 0). This happened only in VC++ 5 or 6, I believe; before that, you had to do the if (0 = myVar) hack to get the compiler to complain. Best wishes, Hans

                      1 Reply Last reply
                      0
                      • L Lost User

                        That means, that using the first method is better to catch typos like "=" that act like assignments? regards

                        G Offline
                        G Offline
                        Gary R Wheeler
                        wrote on last edited by
                        #11

                        Yes. The people who use that method like it for that reason. I don't use the method myself, since it reduces the readability of the conditional expression. For example, the expressions (x == 7) and (7 == x) evaluate the same and produce the same result. If you're reading the code, however, the first expression is read as "if x is equal to 7" and the second expression is read as "if 7 is equal to x". The reading of the second expression seems awkward to me. I prefer to think of testing a variable against a constant.


                        Software Zen: delete this;

                        L J 2 Replies Last reply
                        0
                        • G Gary R Wheeler

                          Yes. The people who use that method like it for that reason. I don't use the method myself, since it reduces the readability of the conditional expression. For example, the expressions (x == 7) and (7 == x) evaluate the same and produce the same result. If you're reading the code, however, the first expression is read as "if x is equal to 7" and the second expression is read as "if 7 is equal to x". The reading of the second expression seems awkward to me. I prefer to think of testing a variable against a constant.


                          Software Zen: delete this;

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          I also prefer the second method (var == 0). dunno why, I just got used to it and find it easier to read. regards

                          1 Reply Last reply
                          0
                          • G Gary R Wheeler

                            Yes. The people who use that method like it for that reason. I don't use the method myself, since it reduces the readability of the conditional expression. For example, the expressions (x == 7) and (7 == x) evaluate the same and produce the same result. If you're reading the code, however, the first expression is read as "if x is equal to 7" and the second expression is read as "if 7 is equal to x". The reading of the second expression seems awkward to me. I prefer to think of testing a variable against a constant.


                            Software Zen: delete this;

                            J Offline
                            J Offline
                            John R Shaw
                            wrote on last edited by
                            #13

                            I have the same problem. It just feels more natural, and easer to read/write (x==7) instead of (7==x). But good habits (a.k.a constant==variable) are good habits. I keep trying to break this one, but habits are haibts.:) Trust in the code Luke. Yea right!

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups