1

I'm learning C++ and java and I've noticed how if you have a conditional, whatever code follows will be executed if the condition is true, even if you don't explicitly write == true

Consider the following very simple C++ code:

bool c_plus_plus_is_cool(){
    return true;
}

int main(){
    if (c_plus_plus_is_cool()) {
        cout << "C++ is cool!";
    } 
    return 0;
}

Q: What is the difference between

if (c_plus_plus_is_cool()) and if (c_plus_plus_is_cool() == true) ?

And is this also the case in other languages (like Java (where I've been writing ==true all this time...)) ?

Chronicle
  • 1,287
  • 3
  • 16
  • 25
  • Even though the question isn't about this, C++ has a very interesting notion of "boolean contexts" (and generally "contextual conversions") which allows all sorts of nice ways of writing `if` statements. – Kerrek SB Dec 08 '13 at 13:22
  • See also http://stackoverflow.com/questions/404838/do-you-prefer-if-var-or-if-var-0/404846#404846 – paxdiablo Dec 08 '13 at 13:25
  • 1
    No point at all, believe it was a self defense habit after many C newcomers kept forgetting what if (0) did... – Tony Hopkinson Dec 08 '13 at 13:25
  • The anserr to this question is very different in C++ and in Java. And especially if you factor in common warnings and the possibility that `c_PLUS_PLUS_IS_cool()` returns something besides `bool`, which is far from obvious at the location where the `if` is. – Yakk - Adam Nevraumont Dec 08 '13 at 14:49

7 Answers7

5

The == operator returns a boolean.
If you already have a boolean, == true will be no different from the original boolean; there is no point in writing it.

Similarly, use the ! operator instead of == false.

One exception to this is C# nullable booleans, where == true will be false for null.

SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896
  • 3
    "`!` instead of `== false`"? Real men use `!(c != false) != true`! – Kerrek SB Dec 08 '13 at 13:21
  • @KerrekSB: Note that the six operations `== true`, `!= true`, `== false`, `!= false`, `== null`, and `!= null` are all valid and different for nullable booleans. – SLaks Dec 08 '13 at 13:25
  • 1
    @KerrekSB: The really real men use : `!(c != (4==5)) != (4!=5)` ;-) – Nawaz Dec 08 '13 at 13:27
  • There's no "C#" among the question tags, alas. In C++, an `std::optional` would have created some amusement, too, had it existed. – Kerrek SB Dec 08 '13 at 13:28
2

Both C++ and Java have the same behaviour in this point and in both languages the == true is just redundant noise.

If you use the verbose syntax in Java so far then this is your own private style. Even in Java this is not the commonly used and accepted/recommended style.

A.H.
  • 57,703
  • 14
  • 82
  • 113
1

There is actually no difference other than syntactical bloat brought by == true.

ScarletAmaranth
  • 4,835
  • 2
  • 20
  • 32
1

In C/C++ and pretty much every language, if and while encapsulate blocks of code that will only be executed if a condition is true. Explicitly putting an == true into your condition statement is redundant but some programmers prefer it; it's basically a style choice and will not affect the behaviour or speed of your program.

wrren
  • 1,231
  • 8
  • 11
1

They both are same. The language implements the conditions as follows:

When it encounters the if statement, please check whether the condition is true. Now, when we say this, it obviously allows you to do == , which the compiler/interpreter will deal it by saying, are the data types or value equal? If yes then proceed further.

Refer the flowchart

The piece of code:

bool c_plus_plus_is_cool(){
    return true;
}

int main(){
    if (c_plus_plus_is_cool()) {
        cout << "C++ is cool!";
    } 
    return 0;
}

is same as:

int main(){
    if (c_plus_plus_is_cool() == true) {
        cout << "C++ is cool!";
    } 
    return 0;
}
Community
  • 1
  • 1
Nagaraj Tantri
  • 4,572
  • 10
  • 47
  • 72
1

No difference when == true is in if or while (or something similar like for or ternary operator), especially if the left value is already of type bool, and in your case it is.

If you need to explicitly convert a value to bool, plain explicit conversion (bool(expr)) looks much better.

THE ONLY case when that strange comparison is reasonable is when the value on the left of == belongs to a class that has a comparison operator taking bool as a second argument (though that generally means bad design:)). But it is not the case, so, to my mind, such coding convention is absolutely useless and rather ugly.

Ellioh
  • 4,642
  • 2
  • 18
  • 31
0

The second one is a bad habit that will give wrong answers in other contexts:

int f() {
    return 3;
}

if (f() == true)
    std::cout << "Won't get here\n";
if (f())
    std::cout << "Will get here\n";
Pete Becker
  • 69,019
  • 6
  • 64
  • 147