ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Avoiding Perpetual Recursions in Programming

Updated on August 24, 2014

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


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


X = X+1

IF X = 20, EXIT


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


IF X/2 is even, PRINT “X is even”

ELSE, PRINT “X is odd”

X = X+1

IF X = 20, EXIT to line 2234


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]


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


    0 of 8192 characters used
    Post Comment
    • profile image


      4 years ago

      What a pleusare to meet someone who thinks so clearly


    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at:

    Show Details
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the or domains, for performance and efficiency reasons. (Privacy Policy)
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
    ClickscoThis is a data management platform studying reader behavior (Privacy Policy)