If Statements

May 6, 2011 at 9:08pm
closed account (zb0S216C)
This has been really bugging me. I see codies using if statements like this:
1
2
3
int Variable( 10 );
if( Variable )
    // do something... 

I don't understand it. What is it testing for? Can anybody explain this?
May 6, 2011 at 9:12pm
It's testing whether Variable's value is non-zero.
Last edited on May 6, 2011 at 9:12pm
May 6, 2011 at 9:15pm
closed account (zb0S216C)
Simple enough. Thanks, Filipe.
May 7, 2011 at 12:38am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int n = ...;
bool b = n; //implicit cast

//now
if( n == 0 )
	assert( b == false );
else
	assert( b == true );

//or
if( b )
	assert( n != 0 );
else
	assert( n == 0 );
Topic archived. No new replies allowed.