if statements in c++ compared to java

This is a discussion on if statements in c++ compared to java within the C++ Programming forums, part of the General Programming Boards category; In java i believe that the 'condition' of an if statement must evaluate to a boolean, is this true as ...

  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
    20,973
    Yes. You may notice a difference in practice because many types (e.g., the integer types) are implicitly convertible to bool in C++.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    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
    20,973
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21