newbie question

J

junky_fellow

Are both of the following expressions equivalent ?
if(x != 0) /* where x is int */
and
if (x)

Similary,
if( !x ) /* whrere x is int */
and
if ( x == 0) are equivalent ?
 
M

Michael Mair

Are both of the following expressions equivalent ?
if(x != 0) /* where x is int */
and
if (x)

Similary,
if( !x ) /* whrere x is int */
and
if ( x == 0) are equivalent ?

Yes.
Note that
- this works not only for numeric types but also for pointers.
- "hard" comparisons make not necessarily sense for floating
point numbers (i.e. not f == 0.0 but fabs(f) <= VERY_SMALL)

Cheers
Michael
 
R

Robert Gamble

Are both of the following expressions equivalent ?
if(x != 0) /* where x is int */
and
if (x)

Similary,
if( !x ) /* whrere x is int */
and
if ( x == 0) are equivalent ?

They are, by definition, equivalent.
This is also true for all scalar types.

Rob Gamble
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top