Thread: if statements in c++ compared to java

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

    if statements in c++ compared to java

    In java i believe that the 'condition' of an if statement must evaluate to a boolean, is this true as well for c++?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Yes. You may notice a difference in practice because many types (e.g., the integer types) are implicitly convertible to bool in C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    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.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by King Mir
    Pointers can also be used -- it works like if(var!=0). It's not really a conversion to bool.
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    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.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ compared to Java
    By ElastoManiac in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 01-10-2007, 06:46 AM
  2. C#, Java, C++
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-05-2004, 02:06 PM
  3. The Java language is being expanded
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 06-11-2004, 09:07 PM
  4. Java woes
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 07-06-2003, 12:37 AM
  5. C or Java as a first language
    By CorJava in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 10-23-2002, 05:12 PM