Thread: super basic question about boolean evaluation

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    super basic question about boolean evaluation

    Code:
    int var = 1;
    if(var == 1)
    {
    
    }
    
    if(var)
    {
    
    }
    Does the first If statement have to do extra work to convert the "var == 1" into a boolean true false? In other words, does the 2nd if statement skip a step and get treated automatically as a boolean, or is there absolutely no difference in the logic?

    is there any speed advantage?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should not worry about micro optimizations. Whether or not there is a speed difference will in 99.9999999999999999% of the cases not matter.
    The two are not equivalent either.

    I wouldn't worry about the details, since you can view them in different ways.
    It's true the second is basically a boolean evaluation. However, it is also possible to view it is if (something == true), where the variable will be converted to a boolean expression. So in that sense, it is just the same as the first.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    Yea, I tend to try to get in the habit of writing like that though ... most of the only reason I program is to solve little programming puzzles

    THANKS!

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    They are not the same.

    If var was a bool it would be perfectly fine to use it alone, in an if statement. Since var is not bool, it has to have a defined conversion to bool operator in order to compile. The int type has such a conversion defined, so it compiles.

    How does an int convert to a bool? Any non-zero value is true.

    The conversion takes place in constant time, and then the evaluation for if occurs. By evaluating var == 1 instead, you will avoid converting to bool, but testing equality for integers also occurs in constant time, so there is no real performance difference. But there is a logical difference.

    If you mean any non-zero value is true, then the only choice present is the first example, but to the second example, only 1 is true.
    Last edited by whiteflags; 02-19-2011 at 03:27 PM.

  5. #5
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    I'd recommend not using anything like:

    Code:
    int var = 1;
    if (var)
    {
        // do stuff
    }
    There's a reason why this kind of thing is illegal in C# and Java - an int is not a boolean and it's hard to read it as though it is one. The only reason C++ supports it is because C++ has to compile C and C has no bool type (well probably also because most early C++ programmers would have been former C programmers and int to bool conversion is a silly idea that C programmers nonetheless have an unnaturally strong affection for).
    Last edited by Mozza314; 02-19-2011 at 06:39 PM. Reason: typo

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is not silly. I see no reason why int -> bool conversion should be prohibited. 0 is false, everything else is true. What is so difficult to understand about it? Obviously there will be some who abuse it, but that's the way it is with most programming languages.
    And btw, I like the int -> bool conversion even though I have never been a C programmer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Well... it's not... too.. bad :-P

    I don't necessary advocate making int -> bool conversion illegal. But I'm definitely in favour of adopting a style that avoids it. The reason why I say it's silly (maybe 'awkward' would be better way to describe it?) is because the way I see it, implicit conversion is for going between two representations of the same thing, so short -> long makes perfect sense to me, int -> bool is weird.

    I guess I'm a platonic idealist at heart. I really like your "The Beautiful C++ Utopia" thingo, but I don't see int -> bool conversion being part of a beautiful utopia.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Elysia View Post
    This is not silly. I see no reason why int -> bool conversion should be prohibited. 0 is false, everything else is true. What is so difficult to understand about it? Obviously there will be some who abuse it, but that's the way it is with most programming languages.
    And btw, I like the int -> bool conversion even though I have never been a C programmer.
    I really don't like the implicit int->bool conversion, and so do the java creators. My understanding goes like this:
    Whenever I see expression:
    Code:
    if (x)
    {
    }
    I read it this way: whether x is valid or not? Whether it succeeded or not?

    Going this way pointer->bool conversion seems to be reasonable. Matter is different when it is an integer. My intuition would suggest that positive integer is valid and negative is not, and it is not true. I always prefer the explicit form for integers, != or >. I know that I test numbers not booleans.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 02:27 AM
  2. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 02:24 AM
  3. Basic C question ---- URGENT
    By x135 in forum Linux Programming
    Replies: 3
    Last Post: 03-25-2006, 11:05 PM