Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Boolean statement (http://www.chiefdelphi.com/forums/showthread.php?t=25303)

blindguyinanorg 14-02-2004 19:40

Boolean statement
 
simple question, in an if statement is a value of 0 = to false or to i need to say !variable==0 to get false( or variable==1). not too important it will just save me time in writing the code and i dont want to go back and have to fix it.

kiracofe8 14-02-2004 20:00

Re: Boolean statement
 
Quote:

Originally Posted by blindguyinanorg
simple question, in an if statement is a value of 0 = to false or to i need to say !variable==0 to get false( or variable==1). not too important it will just save me time in writing the code and i dont want to go back and have to fix it.

int x = 0;
if (x)
//this code never executes
else
//this code always excutes


However, in C, I would consider it to be good coding practice to explicitly write the test as:

if (x == 0)
// stuff
else
// other stuff

or even

if (x != 0)
//other stuff
else
//stuff.

Ryan M. 15-02-2004 07:31

Re: Boolean statement
 
Just to clarify what he said, in C any non-zero value is true. Zero is false. So, if you had a variable and you wanted to test if it was zero, you could say:
Code:

    if(!variableBeingTested)
    {
            // Variable is zero
    }
    else
    {
            // Variable is not zero
    }

It is considered better form by most people, however to explicitly put in the test for zero. For example:
Code:

    if(variableBeingTested == 0)
    {
            // Variable is zero
    }
    else
    {
            // Variable is not zero
    }

--EDIT--
PS I just looked at the last post guy's name and team number and realized he's one of my team's mentors. :)


All times are GMT -5. The time now is 07:22.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi