× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.


  • Subject: DOU/DOW for READ Loops (My final word)
  • From: "Simon Coulter" <shc@xxxxxxxxxxxxxxxxx>
  • Date: Thu, 20 Apr 00 16:31:57 +1000


OK Dan,

You asked for it!
>Damnit Simon, you just had to, didn't you!  OK, _WHY_ is DOU/IF wrong?  None
>of this, "it just is", stuff.  You, of all people, are not a man of few
>words.  (New thread, please.)

I'm not planning to continue this discussion.  No one can persuade me that 
DOU/IF is 
correct so don't try.  I am not expecting to persuade anyoune in that camp they 
are wrong.  
Often times people are wrong and simply cannot see it.  Here is my reasoning, 
think it 
through and decide for yourselves.

0/ Well, it is "just wrong".  Your gut should tell you that!

1/ The most obvious objection is that DOU always executes at least once.  Since 
there is 
no guarantee that the file contains any data (or at least no data that matches 
your search 
criteria), and you shouldn't assume it does, your loop may not execute at all 
therefore a 
DOW is appropriate.

2/ DOU forces an unneccesary additional test for the same condition -- 
regardless of the 
'speed' of the new systems, using excess resources is inefficient and sloppy.

3/ In this case DOU results in contrived code -- what you are attempting to 
code is a DO 
FOREVER then IF EOF LEAVE.  If that's what you want then code it as such.  
Although RPG 
does not provide direct support for that construct (and I don't like it anyway) 
you can 
code:

        'Homer' DOUEQ   'Doh'
                READ    FileFmt         99
        *IN99   IFEQ    *ON
                LEAVE
                ENDIF
                do stuff here
                ENDDO

or in RPGIV

                DOU     'Homer' = 'Doh'
                READ    FileFmt
                IF      %EOF
                LEAVE
                ENDIF
                do stuff here
                ENDDO

In each of these cases the loop is required to run once to determine if data is 
available 
thus a DOU is used.  This seems to me rather artificial.  What you really want 
is WHILE 
DATA DO construct. 

 Languages that do provide proper support for this result in clean, concise, 
elegant code.  
For example in C one could write:

while ((nbrBytesRead = fread(rcdBuffer, sizeof(char), sizeof(rcdBuffer), 
streamFilePtr )) 
> 0 )
{
        /* Do stuff here */
}

This is more elegant because the code states that I want to loop until nothing 
was read 
from the file.  The intent is obvious and it's clean -- I don't need clumsy 
LEAVE or break 
statements but note that the DOU construct would not work without the extra IF 
test.

do 
{
        nbrBytesRead = fread(rcdBuffer, sizeof(char), sizeof(rcdBuffer), 
streamFilePtr );
        /* Do stuff here */
} while ( nbrBytesRead  > 0 ) 

would fail and needs to be 

do 
{
        nbrBytesRead = fread(rcdBuffer, sizeof(char), sizeof(rcdBuffer), 
streamFilePtr );
        if (nbrBytes <= 0) break;
        /* Do stuff here */
} while ( nbrBytesRead  > 0 ) 

which is quite obviously ugly as well as inefficient.  If you don't like the 
break 
statement then recode the if test:

do 
{
        nbrBytesRead = fread(rcdBuffer, sizeof(char), sizeof(rcdBuffer), 
streamFilePtr );
        if (nbrBytes > 0) 
        {
                /* Do stuff here */
        }
} while ( nbrBytesRead  > 0 ) 

Now the double test for nbrBytesRead should be making you cringe.

We know that RPG doesn't directly support reading data and testing the result 
of the read 
in a single line of code but there is no reason why you can't write I/O 
procedures in RPG 
IV that return a boolean or count then you could code something like:

        DOW     (getData(FileFmt))
        do stuff here
        ENDDO

Now is that elegant or what!  Of course, you would want to give a little 
thought to the 
procedure names (well, quite a lot of thought actually -- names are very 
important but 
that's another topic).

4/ As David Keck wrote on MIDRANGE-L -- just do what Simon says! QED

Now for my next exercise in futility I shall debate the merits of using the 
Format name 
rather than the File name on I/O statements ....  :)

Regards,
Simon Coulter.

«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
«» FlyByNight Software         AS/400 Technical Specialists       «»
«» Eclipse the competition - run your business on an IBM AS/400.  «»
«»                                                                «»
«» Phone: +61 3 9419 0175      Mobile:
61 0411 091 400           «»
«» Fax:   +61 3 9419 0175      mailto: shc@flybynight.com.au      «»
«»                                                                «»
«» Windoze should not be open at Warp speed.                      «»
«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
+---
| This is the RPG/400 Mailing List!
| To submit a new message, send your mail to RPG400-L@midrange.com.
| To subscribe to this list send email to RPG400-L-SUB@midrange.com.
| To unsubscribe from this list send email to RPG400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner/operator: david@midrange.com
+---


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.