In java i believe that the 'condition' of an if statement must evaluate to a boolean, is this true as well for c++?
In java i believe that the 'condition' of an if statement must evaluate to a boolean, is this true as well for c++?
Yes. You may notice a difference in practice because many types (e.g., the integer types) are implicitly convertible to bool in C++.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Pointers can also be used -- it works like if(var!=0). It's not really a conversion to bool.
Some classes, such as streams are made to be usable in if statements by implicit casting.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
It is a conversion to bool, unless one explicitly writes var != 0. More generally, all the built-in arithmetic types and pointer types are implicitly convertible to bool.Originally Posted by King Mir
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Hm. A quick test says your right. I didn't think that was the case.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
All conditions in any language are evaluated as statements which result in boolean at the end of the road. Some languages (could even be a level of control from compilers?) do it implicitly, translating statements to express boolean results based on some set of rules. And some languages require the programmer ensures it explicitly, less convenient but prevents some fundamental lines of obscurity.
(One million edits later; writing replies on forums is like writing a damn paper for me, just keep finding better ways of phrasing everything.)
Last edited by since; 11-20-2009 at 01:22 PM.