Program Logic

Unit 6 Index

Dual Decision Structure

Dual Alternative Decision Structure

In a dual alternative decision structure, you handle two situations. If condition “A” does not reconcile to true, then you must do condition “B” The dual alternative decision structure is also known as an "if-then-else" statement. Remember a condition is a Boolean expression that is either true or false. Also remember, the question is being asked, “is this condition true

We use if/else statements when we need to branch our code, in other words, make an either/or type of decision. There is no in-between. You are either able to vote or you are not. Today is either your birthday or it is not. The image below illustrates branching of code. Notice that in both cases, the processing continues outside of the decision structure.

flowchart

When you have a dual alternative decision to make and you are writing the 5-Step Solution Algorithm, you can code the dual alternative decision structure as shown below, where “true condition” is fully stated and run code in both situations is fleshed-out.

IF(true condition){ 
  run the true code
}
ELSE{ 
  run the false code
}

In Java, the anatomy of the if/else statement is coded below. Please review the code below.

if (true condition) {
  // run the true code
}
else{
  // run the false code
}

It is NOT possible for the if code AND the else code to run. This is because whenever a Boolean expression reconciles to true, the if code runs and the else is automatically skipped. Conversely, when the if Boolean expression reconciles to false, the if code is automatically skipped and the else code runs.

Difference Between if and if/else

You may be wondering if you can use two if statements instead of the if/else structure. You could do this without any syntax errors, however, logically you may produce an error. Whenever you have a series of single decision statements each one is always evaluated; therefore, if more than one of the Boolean expressions reconciles to true, the code associated with each one would run.

Logically, you may not want that to happen. Instead, you may want one thing to happen when your condition is true and something totally different to happen when it is false. That is why, for either/or situations, we have the dual alternative decision structure. Study the below code snippets so you fully understand the difference.

// Two stand-alone if statement
int count = 5;

if (count == 5) {
  // count IS 5 so I will do this:
  System.out.println("The count is 5");
}

if (count < 10) {
  // count IS less than 10 so I will do this:
  System.out.println("The count is less than 10");
}

With the two stand-alone if statements, both statements are evaluated. Both are true statements since the value of count is 5. If this code was part of your application both statements would print, which may not be what you want. Now take a look at the if/else code snippets below.

// One if/else  statement
int count = 5;

if (count == 5) {
  // count IS 5 so I will do this:
  System.out.println("The count is 5");
}
else {
  // the exact value is count is not known
  System.out.println("The count is not 5, so the else code runs");
}

With the one if/else statement, first the if condition is evaluated. If that condition is true, then the code within its curly braces runs. In this case, “The count is 5” would be printed to the console. However, if the if condition was not true, then the code within the else curly braces would run. With the if/else statement, only one statement gets printed.

IMPORTANT! Notice there is no parentheses with a condition inside of them for the else part of the structure. That is because it does not matter what the condition is in the else. Remember, this decision structure represents an either/or situation, so basically an else says “all condition above me in this decision structure failed, so I get to run.”

Test Your Knowledge - Interactive Activity

The chapter on single decision structures had several section defining relational operators. Now is the time to see if you remember what they are. Check the box next to each Boolean expression. Remember, a Boolean expression is a statement that can be evaluated to true or false. If you did not get 100% the first time, review the chapter on single decision structures and this chapter, and then take it again.

Check each statement that is a Boolean expression. Press submit to check your answers.

(count == 5)
(3 < 10)
(10 * 20)
(6 < 5)
(count = 5)
(1000)

Detailed Example

Here is a detailed example of using a Dual Alternative Decision Structure. This example contains the 5-Step Solution Algorithm including the Java code. I highly recommend that you create a NetBeans project, type the Java code into it, add the comments, and test the program according to the test plan.

What's next?

After carefully reviewing the detailed example linked above, go to the chapter on Multiple Alternative Decision Structures..