Simple question regarding If and bools.... just to clear any doubts I have!

  • Thread starter Thread starter VMI
  • Start date Start date
V

VMI

I'm having doubts as to how the compiler interprets this If statement:

bool bIsTrue = true;

if (! bIsTrue)
{
//RUN PROCESS
}

Here, will "RUN PROCESS" be executed? Or is this just wrong? How is this
really interpreted by the compiler?

Thanks.
 
Read it like this

if (not True)
//RUN PROCESS

....same as...
if(bIsTrue == false)
//RUN PROCESS
 
Hi VMI,
the if statement will enter the if section only if the conditional
statement evaluates to true.

so if you write:

if(true)
{
//will always come here
}
else
{
//will never get here
}

the top section will always get entered because the conditional always
evaluates to true. Conversely if you write:

if(false)
{
//will never go here
}
else
{
//will always go here
}

the if statement always evaluates to false, so the else will always be
entered.

So for your code:
bool bIsTrue = true;

if (! bIsTrue)
{
//RUN PROCESS
}

this is really saying:

if(NOT TRUE)
{
}

which is equivalent to

if(FALSE)
{
//will never go here
}


Hope that helps
Mark R Dawson
http://www.markdawson.org
 
Back
Top