Methods: Communicating with Objects

3.6 Flow of Control: Control Structures

The Nested if/else Multiway Selection Structure

The statements that one inserts in place of statement1 and statement2 in the if-else statement can be any executable statement, including another if statement or if-else statement. In other words, it is possible to embed one or more if-else statements inside another if-else statement, thereby creating a nested control structure. As with most things, making a control structure too complex isn’t a good idea, but there is a standard nested ifelse control structure that is very useful. It is known as multiway selection. As shown in Figure 3.14, the multiway structure is used when you want to select one and only one option from several alternatives.

Suppose we have an int variable num that will contain one of the values 1, 2, or 3 unless there has been an error assigning a value to it. Suppose that we want to write code that will write out the English word for the value in num. 

Annotation 2020-03-24 135721

Figure 3.14: Flowchart of a nested if-else statement.

In the example shown in Figure 3.14 there are three alternatives plus an error state. Here is the Java code for this example:

Annotation 2020-03-24 135856

Note that the multiway structure has a single entry point and that only one Multiple alternatives of the four possible alternatives is executed. The code will print exactly one of the strings.

We will have many occasions to use the if-else structure. Although it does not represent a significant change, we could rewrite our takeStick() method to make use of the if-else instead of the somewhat obscure statement:

Annotation 2020-03-24 140105

to change the value of player from 1 to 2 or vice versa:

Annotation 2020-03-24 140206

In some respects this version of takeSticks() involves four lines of code instead of one but is simpler to understand. The if-statement tests whether the value of player is 1. If it is, the value is changed to 2. If the value of player is not 1, then the value must be 2 and so the value is changed to 1. Both versions of the code will give precisely the same result, a programmer could choose to write the code either way.


SELF-STUDY EXERCISES 

EXERCISE 3.10 Consider the following method with boolean parameter.

Annotation 2020-03-24 140349

Draw a flowchart for the if-else version of the getStatus() method, using the figures in this section as a guide. The if-else structure should be drawn exactly as shown in Figure 3.11. It should have a single entry point that leads directly to the top of a diamond-shaped box that contains a boolean condition. There should be two branches coming out of the condition box. The one going to the right is the true case, and the one going to the left is the false case. Each of these branches should contain one rectangular box, which contains the statements that would be executed in that case. The left and right branches should be connected by a circular symbol that is aligned directly under the diamond box whose conditions it connects. There should be a single exit arrow pointing directly down. 

EXERCISE 3.11 Identify the error in the following statements:

Annotation 2020-03-24 140600

EXERCISE 3.12 Suppose we have an int instance variable named player in some class describing a three person game. Write a method named getPlayerName() that returns a String. It should return “Ann”, “Bill”, “Cal”, or “Error” when the value of player is respectively 1, 2, 3, or any other value. 

EXERCISE 3.13 How does a parameter for a primitive type differ from a parameter for a reference type?