• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Of Middle-Tested Loops

    July 27, 2005 Hey, Ted

    I recently came across the Recursion and the Alternatives story that you wrote, which gives an example of a program that explodes a bill of material. I noticed that in this program, you used the following statement:

    DOW      '1'
    

    Can you explain what this means? I have been preaching the importance of readability in programs and this baffles me. It seems like it would be better to code something like DOW NOT %EOF(MYFILE). I am actually using a modified version of your “Chase” routine, but this DOW ‘1’ statement bugs me.

    –Eric

    This is not the sort of thing I like to deal with in Four Hundred Guru. I prefer to address practical problems, and how a person codes loops seems like small potatoes to me. However, I’ve received some email about this topic, and this topic shows up occasionally in Web forums, so here goes.

    RPG has been a very successful language because it is suited for business computing tasks, especially file processing and report generation. In all its forms, even indicator-infested RPG II, it stands in contradistinction to academic, scientific, and mathematical languages such as those I have studied, used, and taught. To sum it up in one word, RPG is practical.

    But RPG is not perfect. Consider its looping structures. There are three: a top-tested loop, which controls operations that may not need to be executed at all; a bottom-tested loop, which controls operations that must be executed at least once, and a counted loop.

    But many loops don’t fit these patterns. RPG lacks a middle-tested loop, a structure to control a set of operations that must be executed at least once and a set of operations that may not need to run at all. Look at this section of code from the article Eric mentioned.

    C                   dow       '1'
    C                   read      EndItem
    C                   if        %eof()
    C                   leave
    C                   endif
    C                   callp     Chase (EI_ItemNbr: EI_ItemNbr)
    C                   enddo
    

    This example contains both top-tested and bottom-tested operations. The READ must be done at least once. The CALLP may not run at all.

    The DOW ‘1’ construct sets up an infinite loop. It’s up to the programmer to ensure that at least one LEAVE is coded in the loop somewhere. Consider the alternatives. Here are two.

    C                   read      EndItem
    C                   dow       not %eof(EndItem)
    C                   callp     Chase (EI_ItemNbr: EI_ItemNbr)
    C                   read      EndItem
    C                   enddo
    

    This version uses a top-tested loop, but requires that the READ be duplicated.

    C                   dou       %eof(EndItem)
    C                   read      EndItem
    C                   if        not %eof(EndItem)
    C                   callp     Chase (EI_ItemNbr: EI_ItemNbr)
    C                   endif
    C                   enddo
    

    This version uses a bottom-tested loop with a redundant test for end-of-file. Neither of these seems an improvement to me over the DOW ‘1’ method.

    Another problem is that loops are often terminated by more than one condition. For instance, end-of-file might be the normal way to stop a loop, but a certain error condition might also stop it. DOW NOT %EOF gives the impression that there is only one way to stop the loop. A middle-tested loop would contain two LEAVE operations, which makes it obvious that more than one condition can terminate the loop.


    I’ve coded middle-tested loops various ways and I like this method best of all. Once you get used to it, which doesn’t take long, it’s as intuitive as DOW and DOU.

    I would like to see IBM add another looping structure that could handle top-, bottom-, and middle-tested loops. Here’s a suggestion.

    C                   loop
    C                   read      EndItem
    C                   if        %eof()
    C                   leave
    C                   endif
    C                   callp     Chase (EI_ItemNbr: EI_ItemNbr)
    C                   endloop
    

    I like Eric’s premise, which is that code should be readable. I have to work on the code of people who didn’t share his programming philosophy.

    –Ted

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    VISUAL LANSA 16 WEBINAR

    Trying to balance stability and agility in your IBM i environment?

    Join this webinar and explore Visual LANSA 16 – our enhanced professional low-code platform designed to help organizations running on IBM i evolve seamlessly for what’s next.

    🎙️VISUAL LANSA 16 WEBINAR

    Break Monolithic IBM i Applications and Unlock New Value

    Explore modernization without rewriting. Decouple monolithic applications and extend their value through integration with modern services, web frameworks, and cloud technologies.

    🗓️ July 10, 2025

    ⏰ 9 AM – 10 AM CDT (4 PM to 5 PM CEST)

    See the webinar schedule in your time zone

    Register to join the webinar now

    What to Expect

    • Get to know Visual LANSA 16, its core features, latest enhancements, and use cases
    • Understand how you can transition to a MACH-aligned architecture to enable faster innovation
    • Discover native REST APIs, WebView2 support, cloud-ready Azure licensing, and more to help transform and scale your IBM i applications

    Read more about V16 here.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Cozzi Updates RPG xTools, Partners with Linoma DRV Technologies Gets Hip to Forms

    Leave a Reply Cancel reply

Volume 5, Number 29 -- July 27, 2005
THIS ISSUE
SPONSORED BY:

ProData Computer Svcs
Advanced Systems Concepts
Patrick Townsend & Associates

Table of Contents

  • Of Middle-Tested Loops
  • Use SQL to Easily Update Multi-Key Files
  • Admin Alert: To Each Its Own in Spooled File Management

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Liam Allan Shares What’s Coming Next With Code For IBM i
  • From Stable To Scalable: Visual LANSA 16 Powers IBM i Growth – Launching July 8
  • VS Code Will Be The Heart Of The Modern IBM i Platform
  • The AS/400: A 37-Year-Old Dog That Loves To Learn New Tricks
  • IBM i PTF Guide, Volume 27, Number 25
  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle