Chapter 3.5 – The if Statement | Introduction to Programming Using Java

Chapter 3.5 – The if Statement | Introduction to Programming Using Java

 

3.5  The if Statement

 

The first of the two branching statements in Java is the if statement, which you have already seen in Section 3.1. It takes the form

 

Chapter 3.5 - The if Statement | Introduction to Programming Using Java

 

As usual, the statements inside an if statements can be blocks. The if statement represents a two-way branch. The else part of an if statement—consisting of the word “else” and the statement that follows it—can be omitted.

 

3.5.1 The Dangling else Problem

 

Now, an if statement is, in particular, a statement. This means that either hstatement-1i or hstatement-2i in the above if statement can itself be an if statement. A problem arises, however, if hstatement-1i is an if statement that has no else part. This special case is effectively forbidden by the syntax of Java. Suppose, for example, that you type

 

the if Statement

 

Now, remember that the way you’ve indented this doesn’t mean anything at all to the computer. You might think that the else part is the second half of your “if (x > 0)” statement, but the rule that the computer follows attaches the else to “if (y > 0)”, which is closer. That is, the computer reads your statement as if it were formatted:

 

Chapter 3.5 - The if Statement | Introduction to Programming Using Java

 

You can force the computer to use the other interpretation by enclosing the nested if in a block:

 

Chapter 3.5 - The if Statement | Introduction to Programming Using Java

 

These two if statements have different meanings: In the case when x <= 0, the first statement doesn’t print anything, but the second statement prints “Second case.”.

 

3.5.2  The if…else if Construction

 

Much more interesting than this technicality is the case where hstatement-2i, the else part of the if statement, is itself an if statement. The statement would look like this (perhaps without the final else part):

 

Chapter 3.5 - The if Statement | Introduction to Programming Using Java

 

However, since the computer doesn’t care how a program is laid out on the page, this is almost always written in the format:

 

Chapter 3.5 - The if Statement | Introduction to Programming Using Java

 

You should think of this as a single statement representing a three-way branch. When the computer executes this, one and only one of the three statements—hstatement-1i, hstatement2i, or hstatement-3i—will be executed. The computer starts by evaluating hboolean-expression1i. If it is true, the computer executes hstatement-1i and then jumps all the way to the end of the outer if statement, skipping the other two hstatementis.

If hboolean-expression-1i is false, the computer skips hstatement-1i and executes the second, nested if statement. To do this, it tests the value of hboolean-expression-2i and uses it to decide between hstatement-2i and hstatement-3i.

Here is an example that will print out one of three different messages, depending on the value of a variable named temperature:

 

Chapter 3.5 - The if Statement | Introduction to Programming Using Java

 

If temperature is, say, 42, the first test is true. The computer prints out the message “It’s cold”, and skips the rest—without even evaluating the second condition. For a temperature of 75, the first test is false, so the computer goes on to the second test. This test is true, so the computer prints “It’s nice” and skips the rest. If the temperature is 173, both of the tests evaluate to false, so the computer says “It’s hot” (unless its circuits have been fried by the heat, that is).

You can go on stringing together “else-if’s” to make multi-way branches with any number of cases:

 

Chapter 3.5 - The if Statement | Introduction to Programming Using Java

Chapter 3.5 - The if Statement | Introduction to Programming Using Java

 

The computer evaluates boolean expressions one after the other until it comes to one that is true. It executes the associated statement and skips the rest. If none of the boolean expressions evaluate to true, then the statement in the else part is executed. This statement is called a multi-way branch because only one of the statements will be executed.

The final else part can be omitted. In that case, if all the boolean expressions are false, none of the statements is executed. Of course, each of the statements can be a block, consisting of a number of statements enclosed between { and }. (Admittedly, there is lot of syntax here; as you study and practice, you’ll become comfortable with it.)

 

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 2 – Names and Things | Introduction to Programming Using Java

Chapter 2.5 – Details of Expressions | Introduction to Programming Using Java

Chapter 2.5.1 – Arithmetic Operators | Introduction to Programming Using Java

Chapter 2.5.3 – Relational Operators | Introduction to Programming Using Java

Chapter 2.6.2 – Command Line Environment | Introduction to Programming Using Java

Chapter 2.6.3 – IDEs and Eclipse | Introduction to Programming Using Java

Chapter 3.1.3 – The Basic If Statement | Introduction to Programming Using Java

1 thought on “Chapter 3.5 – The if Statement | Introduction to Programming Using Java”

Leave a Comment