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. ?: operator

?: operator

Scheduled Pinned Locked Moved C / C++ / MFC
question
13 Posts 9 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.
  • G Offline
    G Offline
    greghop
    wrote on last edited by
    #1

    coding style issues, which better, how used ?? 1. if (true == v1) { v2 = 0; } else { v2 = 1; } 2. v2 ((true == v1) ? 0 : 1);

    J B G M 4 Replies Last reply
    0
    • G greghop

      coding style issues, which better, how used ?? 1. if (true == v1) { v2 = 0; } else { v2 = 1; } 2. v2 ((true == v1) ? 0 : 1);

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

      Don't compare a boolean value with true or false. There's no logic in that (sic!). It'll cause more confusion than clarity. For instance:

      if(((enabled == true) == true) == true...)

      Where dou you stop? It's obviously clearer with

      if(enabled)

      But, as the question is formulated, me thinks that

      v2 = v1 ? 0 : 1;

      is best. -- The Blog: Bits and Pieces

      T 1 Reply Last reply
      0
      • G greghop

        coding style issues, which better, how used ?? 1. if (true == v1) { v2 = 0; } else { v2 = 1; } 2. v2 ((true == v1) ? 0 : 1);

        B Offline
        B Offline
        Bob Stanneveld
        wrote on last edited by
        #3

        Hello, It's just a matter of preference. You can also write:

        v2 = !v1;

        Since you can use the opposite value of v1, you can omit v2. This can go on endlessly. Choose what comforts you most. ;) Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

        J 1 Reply Last reply
        0
        • G greghop

          coding style issues, which better, how used ?? 1. if (true == v1) { v2 = 0; } else { v2 = 1; } 2. v2 ((true == v1) ? 0 : 1);

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

          if (v1) {
          v2 = 0;
          }
          else {
          v2 = 1;
          }

          or

          v2 = v1 ? 0 : 1;

          For myself, I only use the condtional operator (? :) when the result is a simple expression.


          Software Zen: delete this;

          1 Reply Last reply
          0
          • J Johann Gerell

            Don't compare a boolean value with true or false. There's no logic in that (sic!). It'll cause more confusion than clarity. For instance:

            if(((enabled == true) == true) == true...)

            Where dou you stop? It's obviously clearer with

            if(enabled)

            But, as the question is formulated, me thinks that

            v2 = v1 ? 0 : 1;

            is best. -- The Blog: Bits and Pieces

            T Offline
            T Offline
            Tim Smith
            wrote on last edited by
            #5

            I just want to add, it can also be a very buggy thing to do. Even a C++ 'bool' can have more values than just true and false. But aside from that, if you are working with Windows API, comparing to TRUE is the wrong thing to do since the API are defined as being 0 and != 0 which means that they might return 5219 for TRUE. Tim Smith I'm going to patent thought. I have yet to see any prior art.

            1 Reply Last reply
            0
            • B Bob Stanneveld

              Hello, It's just a matter of preference. You can also write:

              v2 = !v1;

              Since you can use the opposite value of v1, you can omit v2. This can go on endlessly. Choose what comforts you most. ;) Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

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

              Bob Stanneveld wrote: It's just a matter of preference. You can also write: v2 = !v1; Yes, when v2 is also a bool, but I sensed that the question covered the generic case. -- The Blog: Bits and Pieces

              B 1 Reply Last reply
              0
              • J Johann Gerell

                Bob Stanneveld wrote: It's just a matter of preference. You can also write: v2 = !v1; Yes, when v2 is also a bool, but I sensed that the question covered the generic case. -- The Blog: Bits and Pieces

                B Offline
                B Offline
                Bob Stanneveld
                wrote on last edited by
                #7

                Johann Gerell wrote: Yes, when v2 is also a bool If v2 is an int, true will be changed to 1 and false to 0. Unless v2 is an user defined type, this can work... Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

                1 Reply Last reply
                0
                • G greghop

                  coding style issues, which better, how used ?? 1. if (true == v1) { v2 = 0; } else { v2 = 1; } 2. v2 ((true == v1) ? 0 : 1);

                  M Offline
                  M Offline
                  Maxwell Chen
                  wrote on last edited by
                  #8

                  if(v1) { v2 = 0; }
                  else { v2 = 1; }

                  runs a little faster than

                  v2 = v1 ? 0 : 1;


                  Maxwell Chen

                  T 1 Reply Last reply
                  0
                  • M Maxwell Chen

                    if(v1) { v2 = 0; }
                    else { v2 = 1; }

                    runs a little faster than

                    v2 = v1 ? 0 : 1;


                    Maxwell Chen

                    T Offline
                    T Offline
                    ThatsAlok
                    wrote on last edited by
                    #9

                    Maxwell Chen wrote: if(v1) { v2 = 0; } else { v2 = 1; } runs a little faster than v2 = v1 ? 0 : 1; How thats run Faster?

                    "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                    cheers, Alok Gupta VC Forum Q&A :- I/ IV

                    M T 2 Replies Last reply
                    0
                    • T ThatsAlok

                      Maxwell Chen wrote: if(v1) { v2 = 0; } else { v2 = 1; } runs a little faster than v2 = v1 ? 0 : 1; How thats run Faster?

                      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                      cheers, Alok Gupta VC Forum Q&A :- I/ IV

                      M Offline
                      M Offline
                      Maxwell Chen
                      wrote on last edited by
                      #10

                      About four years ago I did some test about your question with VC++6. Just now I did again with VC++7.1. My method is as:

                      void Test_If(bool& v1, int& v2)
                      {
                      if(v1) {
                      v2 = 0;
                      }
                      else {
                      v2 = 1;
                      }
                      }

                      void Test_Op(bool& v1, int& v2)
                      {
                      v2 = v1 ? 0 : 1;
                      }

                      void CTestComboDlg::OnBnClickedButton1()
                      {
                      bool v1 = true;
                      int v2 = 3;
                      int i;
                      DWORD ds, de;

                      ds = GetTickCount();
                      for(i = 0; i < 0x10000000; i++) {
                      	Test\_If(v1, v2);
                      }
                      de = GetTickCount();
                      TRACE("IF: %u \\n", de - ds);
                      
                      ds = GetTickCount();
                      for(i = 0; i < 0x10000000; i++) {
                      	Test\_Op(v1, v2);
                      }
                      de = GetTickCount();
                      TRACE("OP: %u \\n", de - ds);
                      

                      }

                      The output is:

                      IF: 48690
                      OP: 52576


                      Maxwell Chen

                      T 1 Reply Last reply
                      0
                      • T ThatsAlok

                        Maxwell Chen wrote: if(v1) { v2 = 0; } else { v2 = 1; } runs a little faster than v2 = v1 ? 0 : 1; How thats run Faster?

                        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                        cheers, Alok Gupta VC Forum Q&A :- I/ IV

                        T Offline
                        T Offline
                        toxcct
                        wrote on last edited by
                        #11

                        ThatsAlok wrote: How thats run Faster? maybe because it has to evaluate the ?: operator before.

                        1. using if :

                        • evaluate the condition.
                        • test the if statement
                        • assign v2

                        2. using ?: :

                        • enters the = operator
                        • enters the ?: operator
                        • evaluate the condition
                        • return the right parameter wether the condition is true or false
                        • assign v2

                        i feel it like this... but i personnally still use the second case :-D


                        TOXCCT >>> GEII power
                        [toxcct][VisualCalc]

                        B 1 Reply Last reply
                        0
                        • M Maxwell Chen

                          About four years ago I did some test about your question with VC++6. Just now I did again with VC++7.1. My method is as:

                          void Test_If(bool& v1, int& v2)
                          {
                          if(v1) {
                          v2 = 0;
                          }
                          else {
                          v2 = 1;
                          }
                          }

                          void Test_Op(bool& v1, int& v2)
                          {
                          v2 = v1 ? 0 : 1;
                          }

                          void CTestComboDlg::OnBnClickedButton1()
                          {
                          bool v1 = true;
                          int v2 = 3;
                          int i;
                          DWORD ds, de;

                          ds = GetTickCount();
                          for(i = 0; i < 0x10000000; i++) {
                          	Test\_If(v1, v2);
                          }
                          de = GetTickCount();
                          TRACE("IF: %u \\n", de - ds);
                          
                          ds = GetTickCount();
                          for(i = 0; i < 0x10000000; i++) {
                          	Test\_Op(v1, v2);
                          }
                          de = GetTickCount();
                          TRACE("OP: %u \\n", de - ds);
                          

                          }

                          The output is:

                          IF: 48690
                          OP: 52576


                          Maxwell Chen

                          T Offline
                          T Offline
                          ThatsAlok
                          wrote on last edited by
                          #12

                          Maxwell Chen wrote: he output is: IF: 48690 OP: 52576 Nice, Thanks for same :)

                          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                          cheers, Alok Gupta VC Forum Q&A :- I/ IV

                          1 Reply Last reply
                          0
                          • T toxcct

                            ThatsAlok wrote: How thats run Faster? maybe because it has to evaluate the ?: operator before.

                            1. using if :

                            • evaluate the condition.
                            • test the if statement
                            • assign v2

                            2. using ?: :

                            • enters the = operator
                            • enters the ?: operator
                            • evaluate the condition
                            • return the right parameter wether the condition is true or false
                            • assign v2

                            i feel it like this... but i personnally still use the second case :-D


                            TOXCCT >>> GEII power
                            [toxcct][VisualCalc]

                            B Offline
                            B Offline
                            Blake Miller
                            wrote on last edited by
                            #13

                            You should not have to feel a thing. Just DO IT by examining the disassembly. The assembly dump would explain why one is faster than the other.

                            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