DO LOOPS

General development discussion.

Moderators: Susan Smith, admin, Gabriel

Post Reply
Susan Smith
Posts: 717
Joined: Sun Aug 10, 2008 4:24 am
Location: Southern California

DO LOOPS

Post by Susan Smith »

Hi all,

I haven't used many DO LOOPS before and never one with a file read. I want to change the structure of my file reads from

Code: Select all

00010 start: READ #filenum,using filelayout$:mat data$ eof endfile
00020          ...do some stuff here
00030         goto start
00040 endfile: ! do something else
to a DO LOOP

Do I use LOOP WHILE NOT EOF?
or LOOP UNTIL EOF?
Does EOF even have a meaning in this context?
Where does the DO go - right after the LOOP WHILE statement?
Where does the read statement go in relationship to the DO statement?
Is there still an EOF clause on the read statement when you do it this way?
I obviously don't know what I'm doing and the wiki wasn't much help on this one.

Can someone please rewrite the code snippet that I included here with DO LOOP Logic? I can't seem to get it to work.

Thanks!

-- Susan
Gabriel
Posts: 412
Joined: Sun Aug 10, 2008 7:37 am
Location: Arlington, TX
Contact:

Re: DO LOOPS

Post by Gabriel »

There are several ways to do it.

Code: Select all

00010 READ #filenum,using filelayout$:mat data$ eof Ignore
00020 DO WHILE FILE(FileNum)=0
00030    do stuff here
00040    READ #filenum,using filelayout$:mat data$ eof Ignore
00050 LOOP
or you could do it this way...

Code: Select all

00010 DO 
00020    READ #filenum,using filelayout$:mat data$ eof Ignore
00030    if file(filenum)=0 then
00040       do stuff here
00050    end if
00060 LOOP UNTIL file(filenum)
The BR internal function, FILE, is used here to see if there was an error during the last reading of the file. If it returns 0, the last read was successful.

However, philosophically speaking, I'm not sure weather its simpler to do it with a DO LOOP or with a GOTO statement. Certianly in other languages where error conditions work a different way, it is simpler to use a DO LOOP. But in BR it may be simpler to use a goto statement for this purpose.

Gabriel
Susan Smith wrote:Hi all,

I haven't used many DO LOOPS before and never one with a file read. I want to change the structure of my file reads from

Code: Select all

00010 start: READ #filenum,using filelayout$:mat data$ eof endfile
00020          ...do some stuff here
00030         goto start
00040 endfile: ! do something else
to a DO LOOP

Do I use LOOP WHILE NOT EOF?
or LOOP UNTIL EOF?
Does EOF even have a meaning in this context?
Where does the DO go - right after the LOOP WHILE statement?
Where does the read statement go in relationship to the DO statement?
Is there still an EOF clause on the read statement when you do it this way?
I obviously don't know what I'm doing and the wiki wasn't much help on this one.

Can someone please rewrite the code snippet that I included here with DO LOOP Logic? I can't seem to get it to work.

Thanks!

-- Susan
Susan Smith
Posts: 717
Joined: Sun Aug 10, 2008 4:24 am
Location: Southern California

Post by Susan Smith »

Thanks for the suggestions Gabriel. My goal was to eliminate some of my unnecessary GOTO statements so that my code is more readable and clean. But I can see that in certain places, this might not really provide a significant improvement and I COULD, in fact, muddy it up further.

I'm glad to have these choices. Thanks.

-- Susan
Gabriel
Posts: 412
Joined: Sun Aug 10, 2008 7:37 am
Location: Arlington, TX
Contact:

Post by Gabriel »

I'm of the opinion that its permissible to use a goto statement if the following conditions are all met:

1) you use a label, not a line number
2) there's only one or two goto statements used
3) no spagetti code
4) you're using an external editor so you can save indentation in your code which makes it clear and readable, and you use this indentation to clearly demonstrate the logic flow of the code
5) the goto statement is more simple then any other way of doing it
6) all the goto logic can be viewed at one time on the screen

If we use goto statements carefully they can make code simpler and more readable. But if we use them carelessly then they result in difficult to maintain spagetti code.
Post Reply