What does the following do?

This is a discussion on What does the following do? within the C++ Programming forums, part of the General Programming Boards category; If an "if" is used in the following way what does it do? Code: if(variable_name) { // What to do. ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    62

    What does the following do?

    If an "if" is used in the following way what does it do?

    Code:
    if(variable_name)
    {
    // What to do.
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    variable_name is converted to bool, and then the check for true/false happens.
    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
    May 2010
    Posts
    62
    So it looks if the variable is set?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by Kitt3n
    So it looks if the variable is set?
    No. It converts the variable to type bool. What exactly that means depends on the type of the variable, and its value, e.g., a null pointer is converted to false, but a non-null pointer is converted to true.
    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

Popular pages Recent additions subscribe to a feed

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