Nesting Decision Structures
Single and double branch decision structures are very similar. Typically, if you understand how to implement an if statement with a true branch then implementing the false branch comes pretty naturally. The nesting of decisions structures describes the situation where one or both of your branches contain a new decision structure. We define nesting decision structures as a scenario where one or both of the branches includes a new decision structure. In short, this means one of your if statement branches executes another if statement. There are no practical limitations as to how far this nesting can occur. As long as the syntax rules governing if statements have not been violated, the compiler or interpreter will let you nest decisions structures as many ways and times as you would like. From a practical standpoint as you move to two, three or four levels of nesting, the entire decision structure becomes very complex and difficult to debug. Just as we took complex calculations and broke them up into smaller calculations the same benefits would apply to breaking large complicated if blocks in to multiple smaller if blocks.
Key Concept: Now that we have discussed decision structures, you see that a decision implemented in a computer program can only have two outcomes. It can be true or false or yes or no (whatever you prefer to use). This is pretty primitive. Hard to understand how this is very powerful but it is. What you can do with decisions is nest them into a series of true/false decisions. Complex decisions are implemented as a series of simple true/false decisions. Since the computer can execute decisions very quickly and can keep all of the outcomes organized, it can become a very used tool for automated decision making even with the limitation of only being able to support two outcomes.
Flowchart Example of nested Decision Structures
The flowchart rules for nested decision structures are the same as the rules for single and double branch decision structures. In our flowchart, we would see the nested decision structure appear as a second diamond hanging off the point of another diamond. The first diamond would represent the primary decision structure and the second decision structure and would represent the nested one. I have drawn an example of this and figure 11.
Figure 11: IF Statement with nested IF statement
Pseudo Code Example of nested Decision Structures
All of the rules that applied with single and double branch pseudo code also apply to the nesting of if statements. In the example below, I have nested an if decision statement off of the true and false branch of the primary if statement.
booksQty equals 3
if booksQty >= 3 then
if loyalty = true then
discount = discount plus .003
else discount equals .005 if loyalty = true then discount = discount plus .001 totCost equals totCost less (totCost times discount) print totCost
This is the first decision structure (implemented as an if statement).
Here we have the second nested decision structure (also implemented as an if statement).
In Practice: Business Rules - A common expression used to describe business logic as business rules. A business rule is computer logic that defines some set of program structures used by an organization in the day to day operation of their business. For us, we could look at the loyalty purchase program used by Cactus Books and Tapes as a business rule. It qualifies as a business rule because it's a set of logic which defines the discount given to a customer who makes frequent purchases. We know from our previous work in lessons two and three that this business rule can be converted into a logic model and implemented via a program. This is just one business rule of many at Cactus Books and Tapes. Business rules are defined by the end user during the requirements phase of SDLC and implemented as a logic model first and then program code in the design phase.. The next time someone talks about a business rule you'll know exactly what they're referring to and how it is implemented in a computer program.
Why do not all if statements have a false branch?
Why must a sequence structure have a line draw into and from the sequence structure symbol?
What does nesting of structures mean?