Avoiding Perpetual Recursions in Programming
Just about every programmer, in any generation of programming language, has dreaded the continuous loop. He cringes when there is a call to his workstation from the mainframe manager complaining the entire mainframe is locked up or the remote printer manager screaming that they have gone through a forest of paper. No matter how well a programmer thinks it out, the perpetual rondo can occur.
The simple cure for this fear is not to trust one’s instincts, but rather to build the loop by steps and test it every inch of the way. The first step is to create it without a recursion.
In pseudo-language:
Define X [say as one}
FOR X = 1
PRINT (or DISPLAY) “X = “ X
If you do not return to the FOR statement, the loop ends here.
The most trustworthy approach is this one of defining X before entering the loop.
Next step is to create an exit line: IF X = 20, EXIT. This is where you define the specific condition which should end the loop. Now you can start iterations:
Define X [say as one}
FOR X = 1 to 25
PRINT (or DISPLAY) “X = “ X
X = X+1
IF X = 20, EXIT
NEXT FOR
So far, you are checking to see if X is incrementing and if the exit statement is working by the printouts. The EXIT statement might be to “jump” to a specified line or module. You must be sure that this line or module exists, if even as a stub, before going on. And at the EXIT, have a print statement to ensure that the exit has occurred.
Once the basic loop has been defined, IF-THEN statements, changes to the value of X and other internal loops can be added, each time with print statements and exits to ensure that the loop is performing as it is supposed to. Whether you are using numbers, dates, weights, or even text variables, the process is the same.
For instance, if you were trying to separate odd numbers from even numbers, the pseudo code might look like this:
Define X [say as one}
FOR X = 1 to 25
PRINT (or DISPLAY) “X = “ X
IF X/2 is even, PRINT “X is even”
ELSE, PRINT “X is odd”
X = X+1
IF X = 20, EXIT to line 2234
NEXT FOR
LINE 2234: PRINT “loop is complete”
Between the FOR statement and the escape line, just about anything can be done, including moving to a separate module for manipulation and returning to the internal part of the loop. At every step, set up a print statement to verify that your logic is working properly before adding another wrinkle.
Within that loop, you can evaluate more than one variable, compare variables, read variable values from a table and perform extra calculations before going on to the next iteration. For example, you could use this type of loop to collect an employee’s clock-in and clock-out time; compare the date to a calendar to see if it occurs on a weekend; subtract clock-in from clock-out to get actual hours worked. Multiply this by 1.5 if it’s a Saturday or by 2 if it’s a Sunday, and then load those hours onto an array. Increment to the next day (assuming the loop is Sunday through Saturday) and do the same. Add up all seven counts and you have the employee’s pay hours for the week. At that point the loop ends. Once you have the loop fully developed, use sample data for an employee which includes Saturday and Sunday work hours as well as a blank day, to be sure your loop can handle that. The final module would look like this:
X = 1
Y = Sunday’s date
Z’ = clock in time; Z’’ = clock-out time
Totaltime = 0
FOR X = 1 to 7
Read Z’ and Z’’ for Y
daystime = Z’’ – Z’
If Y = Saturday, daystime = daystime * 1.5
ELSE if Y = Sunday, daystime = daystime * 2
totaltime = totaltime + daystime
Print “X = “ X; Y’s weekday, daystime, totaltime
X = X+1
Y = Y + one day
daystime = 0 [this step may be dropped, but better keep it in till all else is checked]
IF X = 8, exit loop [this would probably go to a module where the data for the employee would be stored and the data for the next employee would be read, then return to the X=1 line]
NEXT X
Notice that the resetting of the variables does not occur until the loop has completed. Once you have confirmed that the recursion is working correctly, you can remove all the test PRINT statements; run the loop again to be sure you didn’t miss any.
The most important things are to add each calculation, change or move one at a time and test it with a print statement, and to be sure that your test data covers every possible occurrence that could happen, including typos. By following these suggestions, you should not create a continuous loop.
© 2014 Bonnie-Jean Rohner