7.3 — Common if statement problems

This lesson is a continuation of lesson 7.2 -- If statements and blocks. In this lesson, we’ll take a look at some common problems that occur when using if statements.

Nested if statements and the dangling else problem

It is possible to nest if statements within other if statements:

The above program introduces a source of potential ambiguity called a dangling else problem. Is the else statement in the above program matched up with the outer or inner if statement?

The answer is that an else statement is paired up with the last unmatched if statement in the same block. Thus, in the program above, the else is matched up with the inner if statement.

To avoid such ambiguities when nesting if statements, it is a good idea to enclose the inner statement within a block. Here is the above program written without ambiguity:

Now it is much clearer that the else statement belongs to the inner if statement.

Encasing the inner if statement in a block also allows us to explicitly attach an else to the outer if statement:

The use of a block tells the compiler that the else statement should attach to the if statement before the block. Without the block, the else statement would attach to the nearest unmatched if statement, which would be the inner if statement.

Using logical operators inside the conditional

Remember that the conditional of an if statement is just an expression that evaluates to a Boolean value. We can simplify the above example by using logical operators (covered in lesson 5.7 -- Logical operators) inside the conditional:

This helps avoid the nested if-statement, and code that is less nested is less error prone.

Here’s another example:

Null statements

A null statement is a statement that consists of just a semicolon:

Null statements do nothing. They are typically used when the language requires a statement to exist but the programmer doesn’t need one. For readability, null statements are typically placed on their own lines.

We’ll see examples of intentional null statements later in this chapter, when we cover loops. Null statements are rarely intentionally used with if statements. However, they can unintentionally cause problems for new (or careless) programmers. Consider the following snippet:

In the above snippet, the programmer accidentally put a semicolon on the end of the if statement (a common mistake since semicolons end many statements). This unassuming error compiles fine, and causes the snippet to execute as if it had been written like this:


Be careful not to “terminate” your if statement with a semicolon, otherwise your conditional statement(s) will execute unconditionally (even if they are inside a block).

Operator== vs Operator= inside the conditional

Inside your conditional, you should be using operator== when testing for equality, not operator= (which is assignment). Consider the following program:

This program will compile and run, but will produce the wrong result in some cases:

Enter 0 or 1: 0
You entered 1

In fact, this program will always produce the result You entered 1. This happens because x = 0 first assigns the value 0 to x, then evaluates to the value of x, which is now 0, which is Boolean false. Since the conditional is always false, the else statement always executes.

7.4 -- Switch statement basics
7.2 -- If statements and blocks

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">