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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. if(BOOL_b) vs if(BOOL_b == FALSE)

if(BOOL_b) vs if(BOOL_b == FALSE)

Scheduled Pinned Locked Moved C / C++ / MFC
visual-studioquestion
5 Posts 5 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.
  • F Offline
    F Offline
    fitatc
    wrote on last edited by
    #1

    BOOL b
    A. if(!b){ ... }
    B. if(b == FALSE){ ... }

    There is my senior who says that B is better though said by another senior that a is better, too. Both could not exactly explain the resion.:~ Also, sample by MSDN like PtInRect's, is test by if(b). And I checked the assembly of A and B, they are the same. :confused: Which is better in your opinion, A or B? Is it really different only in oppearance?

    M N E N 4 Replies Last reply
    0
    • F fitatc

      BOOL b
      A. if(!b){ ... }
      B. if(b == FALSE){ ... }

      There is my senior who says that B is better though said by another senior that a is better, too. Both could not exactly explain the resion.:~ Also, sample by MSDN like PtInRect's, is test by if(b). And I checked the assembly of A and B, they are the same. :confused: Which is better in your opinion, A or B? Is it really different only in oppearance?

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

      Sorry I did not read through the end of your description earlier. The if evaluates true/false. Case A:

      bool b; // b is a boolean.
      if(!b) { }

      Case B:

      BOOL bb; // bb is an integer.
      if(FALSE == bb) { }

      That's what I prefer... ;)

      Maxwell Chen

      1 Reply Last reply
      0
      • F fitatc

        BOOL b
        A. if(!b){ ... }
        B. if(b == FALSE){ ... }

        There is my senior who says that B is better though said by another senior that a is better, too. Both could not exactly explain the resion.:~ Also, sample by MSDN like PtInRect's, is test by if(b). And I checked the assembly of A and B, they are the same. :confused: Which is better in your opinion, A or B? Is it really different only in oppearance?

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        fitatc wrote:

        Which is better in your opinion, A or B?

        I think it's a matter of style. I like A. :)

        Navaneeth How to use google | Ask smart questions

        1 Reply Last reply
        0
        • F fitatc

          BOOL b
          A. if(!b){ ... }
          B. if(b == FALSE){ ... }

          There is my senior who says that B is better though said by another senior that a is better, too. Both could not exactly explain the resion.:~ Also, sample by MSDN like PtInRect's, is test by if(b). And I checked the assembly of A and B, they are the same. :confused: Which is better in your opinion, A or B? Is it really different only in oppearance?

          E Offline
          E Offline
          Eytukan
          wrote on last edited by
          #4

          As Max has pointed out, when it's a true/false case, it's recommended that you use bool and not BOOL. Worse is to use a BOOL and store integer values in it. Just like the MS people do. :doh: Also to reply your OQ, It's not a preference, you should always use

          if(cond)
          {

          }

          Rather than

          if(cond==true)
          {

          }

          Because with the later, there are chances you end up with the legendary "=" operator. like if(cond=true).

          He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus

          1 Reply Last reply
          0
          • F fitatc

            BOOL b
            A. if(!b){ ... }
            B. if(b == FALSE){ ... }

            There is my senior who says that B is better though said by another senior that a is better, too. Both could not exactly explain the resion.:~ Also, sample by MSDN like PtInRect's, is test by if(b). And I checked the assembly of A and B, they are the same. :confused: Which is better in your opinion, A or B? Is it really different only in oppearance?

            N Offline
            N Offline
            Niklas L
            wrote on last edited by
            #5

            Always try to use the first form. Checking for equality with FALSE is theoretically ok, but you can end up in trouble if you compare it with TRUE. FALSE is always zero, but any other value is 'true'.

            BOOL b = 3;

            if (b) {}
            if (b == TRUE) {}

            shows the problem. (The first if fires, but not the second one) My advice is to use the first form, which is more elegant, and always safe. Also think about how you name your variables, and it will be easier to read the code.

            home

            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