Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

15
Mar

A tip for writing conditional statements

Related Blog Items

Did you know that the following if statement is semantically right, but could logically be wrong?

    if ( i = 2 )
    {
        /* do something */
    }   

Tip:

The compiler may only produce a waring: "Possibly incorrect assignment", but we may ignore it.  To avoid such a mistake, just reverse the two identifiers.

    if ( 2 == i )
    {
        /* do something */
    }

If ‘==’ is replaced by an ‘=’, the compiler will give an error saying "Lvalue required".

Note:  This post is just to provide a hint and may not suit to your coding style.

Popularity: 5%

You need to log on to convert this article into PDF


Related Blog Items

No Comments

No comments yet.

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image