C program coding to recognize nested if control statement and display number of levels of nesting
The various combinations of these attributes may result in code that looks something like this: sub calcPay(emp) // Calculate pay subroutine if emp.isManager // field reference if emp.isExempt if emp.isPartTime foo_1 else foo_2 end if else // non-exempt if emp.isPartTime foo_3 else foo_4 end if end if else // non-mgr if emp.isExempt if emp.isPartTime foo_5 else foo_6 end if else // non-exempt if emp.isPartTime foo_7 else foo_8 end if end if end if end sub Here we have 8 different "cells" based on all possible combinations of the 3 attributes. (Note that some cells may be error messages for invalid combinations.) Variations of this structure are not uncommon. Thus the question: how does one "fix" it? One approach is to convert every IF block into a separate subroutine. sub calcPay() if emp.isManager doManager() else // non-mgr doNonManager() end if end sub //----------------- sub doManager() if emp.isExempt doMgrExempt() else // non-exempt doMgrNonExempt() end if end sub //----------------- sub doMgrExempt() if emp.isPartTime foo_1 else foo_2 end if end sub //----------------- sub doMgrNonExempt() if emp.isPartTime foo_3 else foo_4 end if end sub // etc.....
159 views
Usually answered in minutes!
×