If else statements are one of the most fundamental control structures available in C++ and Java. They allow the program to follow one of two paths depending on the outcome of a logical expression. The structure can be summarised as

if (condition) {
 some_actions();
} else {
 other_actions();
}

If statements can be nested like this

if (condition1) {
 some_actions();
} else if (condition2) {
 other_actions();
} else {
 even_other_actions();
}

The concept of the if statement is so simple that it is surprising that there are a number of important issues related to it. In this series I will highlight some of the problems and issues related to the humble if statement.

In this first instalment, I will focus on a simple method that helps avoiding silly mistakes. These mistakes are easy to make and can be very hard to spot. Almost any seasoned programmer will be able to tell you a story about how this mistake has cost him many days worth of valuable working time and tested his patience and endurance to the utmost.

The mistake is related to a simple typo in the code and the fact that the comparison operator == is very similar to the assignment operator =. Consider the following – faulty – piece of code

int a=calculate_something();
if (a=0) {
 some_actions();
}

The problem with this bit of code is that the if statement does not test for equality between a and zero, but rather assigns zero to a, thereby changing the value of a. The correct bit of code would have been

int a=calculate_something();
if (a==0) {
 some_actions();
}

In a large piece of software such a little difference is almost impossible to spot.
What is worse is that the compiler won’t complain either. The result of an assignment is the value that has just been assigned. (To be correct, it is a reference to the variable a after the assignment.) Because the if statement will happily accept integers, the above is like saying if (0) or in boolean terms, if (false). The effect of this is that the if block will never be executed.

How can you avoid such an easy to make mistake? The answer is to force yourself to always write the logical condition in the other order. Instead of the above you get into the habit of writing

if (0==a) {
 some_actions();
}

If you then accidentally forget one = you will get

if (0=a) {
 some_actions();
}

The compiler will complain about this piece of code, because you can’t assign a value to the literal constant 0. So far so good, but comparisons will not always compare to a literal value. If you compare against an expression then the answer is straightforward: place the expression on the left and the variable to the right, i.e. instead of

if (a== b+c)

write

if (b+c==a)

Again, you can’t assign a value to b+c, and the compiler will complain if you forget one equal sign.

Tagged with:
 

One Response to If Statements Part 1:
Avoiding Silly Errors

  1. world cup jersey 2014

    Bloggers blog stir one’s stumps superbly, hoping to swap the next equal up, your connector is consenting, this is me, affect combine: http://www.cheapworldcupjersey.com, Location:USA

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>