|
Home > Archive > Programming with dBASE > December 2005 > Using "Exit" and "For...Endfor" inside nested loops?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Using "Exit" and "For...Endfor" inside nested loops?
|
|
| Pieter van Heerden 2005-12-28, 7:23 am |
| Using "Exit"
I found that if I use "Exit" in the inner loop of nested loops, "Exit" takes one out of all loops and resumes the program on the code line after the end of the outer loop. I have always been under the impression that "Exit" is supposed to take one out of
the current loop only. Is this a misunderstanding of the use of "Exit " on my part or is it a misbehaviour by the "Exit" statement? See the following:
Loop1
Loop2
Program code
...
....
If statement
Exit
endif
Endloop2
Endloop1
Next program line
In the above construct program action jump to the "Next program line" after "Endloop1 without completing the iterations required by Loop1.
Using "For.....Endfor"
Using "For...endfor" in nested loops gives a result somewhat similar to the above. The loop within which the "For...Endfor" construct lies is terminated and the next outer loop resumes where it has left off. The loop within which the "For...Endfor cons
truct lies does not complete its iterations. Is this supposed to be so?
Loop1
Loop2
Program code
...
....
For x = 1 to n
Do whatever
endfor
Endloop2
Endloop1
In this case the Endfor results in the "skipping" of the rest of Loop2 and the resumption of the program on the line after "Endloop2", even though the iterations of Loop2 have not yet been completed.
Funnily enough, I have used For...Endfor constructs in non-nested loops, and in these cases behaviour was as expeted, i.e. the Loop construct completed its iterations!
| |
| Robert Bravery 2005-12-28, 7:23 am |
| HI,
I think your understanding of the exit statement is correct, well at least
it is the same as mine.
Are you sure you are exiting out of both loops and no that the outside loop
criteare is not met anymore so it exits itself
Consider the following
x = 0
n=0
do while x <1
do while n <10
n++
x++
if x = 1
exit
endif
enddo
enddo
Robert
"Pieter van Heerden" <psvh@mweb.co.za> wrote in message
news:$EYz7X5CGHA.1296@news-server...
> Using "Exit"
> I found that if I use "Exit" in the inner loop of nested loops, "Exit"
takes one out of all loops and resumes the program on the code line after
the end of the outer loop. I have always been under the impression that
"Exit" is supposed to take one out of the current loop only. Is this a
misunderstanding of the use of "Exit " on my part or is it a misbehaviour by
the "Exit" statement? See the following:
>
> Loop1
> Loop2
> Program code
> ...
> ....
> If statement
> Exit
> endif
> Endloop2
> Endloop1
> Next program line
>
> In the above construct program action jump to the "Next program line"
after "Endloop1 without completing the iterations required by Loop1.
>
>
> Using "For.....Endfor"
> Using "For...endfor" in nested loops gives a result somewhat similar to
the above. The loop within which the "For...Endfor" construct lies is
terminated and the next outer loop resumes where it has left off. The loop
within which the "For...Endfor construct lies does not complete its
iterations. Is this supposed to be so?
>
> Loop1
> Loop2
> Program code
> ...
> ....
> For x = 1 to n
> Do whatever
> endfor
> Endloop2
> Endloop1
>
> In this case the Endfor results in the "skipping" of the rest of Loop2 and
the resumption of the program on the line after "Endloop2", even though the
iterations of Loop2 have not yet been completed.
>
> Funnily enough, I have used For...Endfor constructs in non-nested loops,
and in these cases behaviour was as expeted, i.e. the Loop construct
completed its iterations!
>
| |
| Pieter van Heerden 2005-12-28, 11:23 am |
| I am sure that in the case I have refered to, that the exit out of all "loops" happened when there were still loops to be done by the outer loop!
Robert Bravery Wrote:
> HI,
>
> I think your understanding of the exit statement is correct, well at least
> it is the same as mine.
> Are you sure you are exiting out of both loops and no that the outside loop
> criteare is not met anymore so it exits itself
>
> Consider the following
> x = 0
> n=0
> do while x <1
> do while n <10
> n++
> x++
> if x = 1
> exit
> endif
> enddo
> enddo
>
> Robert
>
>
> "Pieter van Heerden" <psvh@mweb.co.za> wrote in message
> news:$EYz7X5CGHA.1296@news-server...
> takes one out of all loops and resumes the program on the code line after
> the end of the outer loop. I have always been under the impression that
> "Exit" is supposed to take one out of the current loop only. Is this a
> misunderstanding of the use of "Exit " on my part or is it a misbehaviour by
> the "Exit" statement? See the following:
> after "Endloop1 without completing the iterations required by Loop1.
> the above. The loop within which the "For...Endfor" construct lies is
> terminated and the next outer loop resumes where it has left off. The loop
> within which the "For...Endfor construct lies does not complete its
> iterations. Is this supposed to be so?
> the resumption of the program on the line after "Endloop2", even though the
> iterations of Loop2 have not yet been completed.
> and in these cases behaviour was as expeted, i.e. the Loop construct
> completed its iterations!
>
>
| |
| Mervyn Bick 2005-12-28, 1:23 pm |
| On Wed, 28 Dec 2005 12:03:42 +0200, Pieter van Heerden <psvh@mweb.co.za>
wrote:
> Using "Exit"
> I found that if I use "Exit" in the inner loop of nested loops, "Exit"
> takes one out of all loops and resumes the program on the code line
> after the end of the outer loop. I have always been under the
> impression that "Exit" is supposed to take one out of the current loop
> only. Is this a misunderstanding of the use of "Exit " on my part or is
> it a misbehaviour by the "Exit" statement? See the following:
There was a discussion a month or so ago about EXIT not behaving as
expected when used in DO UNTIL loops although it behaves as expected in DO
WHILE loops. Apparently the QAID number for the bug report is 3918. As
nothing was said about nested loops you may have unearthed a further
anomaly. You should perhaps take this further in the bug-report NG.
As a work-around try setting the loop limit to its maximum, and if
neccessary do both loops at the same time, instead of using EXIT. This
will cause the loop to close naturally. (This is also in accordance with
the concept of structured programming only allowing one entry point and
one exit point for a routine. Using EXIT creates a second exit point and
used to be considerd a no-no by purists.)
Mervyn.
|
|
|
|
|