Adding complexity, but not necessarily difficulty
This topic is short, but necessary for picking up a couple of loose ends. Nested branching will be discussed here. Once through this topic, you can pursue some extended branching conditions such as using short circuiting and forcing a decision from a given branch point.
Nesting if Statements
As mentioned earlier, it is also possible to nest if statements if this seems appropriate. Nesting is the use of one operation inside the result block of another. An example of a nested if condition is shown below.
// check for older than 67
if( age > 67 )
{
// check for more than 20 work years
if( workYears > 20 )
{
// set social security benefits flag
socialSecurityBenefits = true;
}
}
The first thing you notice here is the benefit of both indenting and using the curly braces. This is still a simple operation, but the code and the decision making processes are so much easier to analyze than going without the curly braces. The next thing you notice is that you could have accomplished the above with a logical condition, as shown below.
// check for older than 67 and more than 20 years at work
if( ( age > 67 ) && ( workYears > 20 ) )
{
// set social security benefits flag
socialSecurityBenefits = true;
}
However, there are conditions where one branch will lead to other groups of branches. Consider the medical example process below. Design statements are not provided here since this is a self-documenting demonstration
if( throatIsSore == true )
{
// Response to sore throat
if( hasFever == true )
{
// Response to has fever
if( ( virusInfection != true ) && ( allergicToPenicillin != true ) )
{
// Response if person doesn't have
// a virus infection
// and is not allergic to penicillin
prescribeAntibiotics = true;
}
else
{
// Response if this is a virus infection
// or the person is allergic to penicillin
prescribeBedRest = true;
}
}
else
{
// Response if the person does not have a fever
prescribeWarmLiquids = true;
}
}
The first thing to note here is the clarity of the decision making, and layers of decision making, created by the clean and appropriate indenting. There are many ways to indent that would remove this clarity. This is just one of many good reasons to keep your curly braces aligned vertically.
You should be able to see that there are several ways to approach a logical condition. Note that this decision making process is shown as a programming problem. However, this is a very simple version of the decision making process implemented by medical professionals. Everyone uses problem solving processes; successful people use logical and scientific problem solving processes, whether or not they program computers.
Nesting if and other branching statements can be effective and helpful. However, there are many times that thinking through the logic more thoroughly will eliminate the need for nesting. While nesting is a good tool to have around, use it sparingly. Most times you will not go more than two levels in a nesting process, and three levels is pretty unusual. If you find yourself setting nesting further than three levels down, stop and review your logic. In most cases, there is a better, and simpler way that is less prone to error.
This brief topic demonstrated the use of nesting with if statements. As you observed, it is not complicated, and it is eminently more readable and manageable as long as you use appropriate indenting and keep the curly braces aligned.
Watch this video to see and work with an example of if statement nesting.