For Next Loops
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by blueb.
I've done it with a Goto Label:
e.g.
Is this considered "healthy"?
--blueb
I've done it with a Goto Label:
e.g.
Code: Select all
Procedure.s PackDir(SourceDir$)
a$ = SourceDir$
For x = Len(a$) To 1 Step - 1
cur$=Mid( a$, x, 1)
If cur$ = "\":
; Get out now!!!!
Goto GetOut
EndIf
ans$ = cur$ + ans$
Next
GetOut:
ProcedureReturn ans$
EndProcedure
;Main
ans$ = PackDir("c:\Job\BobJob\HarryGuy\Yesterday\BigJob")
MessageRequester("", ans$,0)
--blueb
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Berikco.
if you want to exit the procedure, this works to, no need for goto.
;Main
ans$ = PackDir("c:\Job\BobJob\HarryGuy\Yesterday\BigJob")
MessageRequester("", ans$,0)
Regards,
Berikco
http://www.benny.zeb.be
if you want to exit the procedure, this works to, no need for goto.
Code: Select all
Procedure.s PackDir(SourceDir$)a$ = SourceDir$
For x = Len(a$) To 1 Step - 1
cur$=Mid( a$, x, 1)
If cur$ = "\":
; Get out now!!!!
;Goto GetOut
ProcedureReturn ans$
EndIf
ans$ = cur$ + ans$
Next
GetOut:
ProcedureReturn ""
EndProcedureans$ = PackDir("c:\Job\BobJob\HarryGuy\Yesterday\BigJob")
MessageRequester("", ans$,0)
Regards,
Berikco
http://www.benny.zeb.be
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by blueb.
Thanks Berikco,
I wasn't sure that I could have 2 ProcedureReturns in the
same procedure. It works, so that's good.
I didn't like 'Goto' inside a For:Next loop. It
doesn't seem right. {grin}
One way to solve all of these little problems would be to
have a universal "Exit Now" function, like FakeReturn
Maybe Fred can have the FakeReturn verb used for all
loops such as: For:Next, While:Wend, Repeat:Until, etc.
It would return the program to the line following the loop.
thanks again,
--blueb
Thanks Berikco,
I wasn't sure that I could have 2 ProcedureReturns in the
same procedure. It works, so that's good.
I didn't like 'Goto' inside a For:Next loop. It
doesn't seem right. {grin}
One way to solve all of these little problems would be to
have a universal "Exit Now" function, like FakeReturn
Maybe Fred can have the FakeReturn verb used for all
loops such as: For:Next, While:Wend, Repeat:Until, etc.
It would return the program to the line following the loop.
thanks again,
--blueb
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Jose.
How about having a Break for the For Loop exit as is in other languages.
I think that using a GOTO is bad practice as it makes code unreadable, if you must break out of a FOR/NEXT why not use a Repeat/until with a counter, then just set the counter to the exit value;
nCount = 0
Repeat
If nCount > 5 ; some condition
nCount = 999 ; or some other value
EndIf
nCount = nCount + 1
Until nCount >= 10
Just my thoughts
How about having a Break for the For Loop exit as is in other languages.
I think that using a GOTO is bad practice as it makes code unreadable, if you must break out of a FOR/NEXT why not use a Repeat/until with a counter, then just set the counter to the exit value;
nCount = 0
Repeat
If nCount > 5 ; some condition
nCount = 999 ; or some other value
EndIf
nCount = nCount + 1
Until nCount >= 10
Just my thoughts
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by blueb.
Jose,
For:Next is the only looping device that allows
a negative STEP. (a least according to the help file)
I don't believe Repeat:Until will do what I needed.
--blueb
Code: Select all
For x = Len(a$) To 1 Step - 1
cur$=Mid( a$, x, 1)
If cur$ = "\":
; Get out now!!!!
Goto GetOut
EndIf
ans$ = cur$ + ans$
Next
For:Next is the only looping device that allows
a negative STEP. (a least according to the help file)
I don't believe Repeat:Until will do what I needed.
--blueb
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by tinman.
--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.30)
Loops all work the same way - they loop while or until a condition is met. Therefore all loops are interchangable, although some are more appropriate depending on what type of exit checks you must perform and when the loop must be executed (e.g. always executed at least once - Repeat...Until, or conditionally executed the first time - While...Wend).Originally posted by blueb
For:Next is the only looping device that allows
a negative STEP. (a least according to the help file)
I don't believe Repeat:Until will do what I needed.
Code: Select all
For i=50 To 1 Step -1
; exit early
i = 1
Next
; Repeat might not be the best choice here since you are not
; always guaranteed to have 1 character in the string and therefore
; the first execution of this loop may be in error
i = 50
Repeat
; exit early
i = 1
; Loop condition
i = i - 1
Until i = 1
i = 50
While i>=1
; exit early
i = 1
; Loop condition
i = i - 1
Wend
--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.30)
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by MrVainSCL.
Hi @ all
I thought a litte moment when i saw blueb first code-snip and then i become an idea (havent tested yet if it work or not... but it should work i hope when i dont make a mistake in this hurry...
I am to tired to have a clean head and think atm
n8
PIII450, 256MB Ram, 80GB HD + 6,4 GB, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...
greetz
MrVainSCL! aka Thorsten
Hi @ all
I thought a litte moment when i saw blueb first code-snip and then i become an idea (havent tested yet if it work or not... but it should work i hope when i dont make a mistake in this hurry...
Code: Select all
Procedure.s PackDir(SourceDir$)
a$ = SourceDir$
tmp = Len(a$)
;
For x = tmp To 1 Step - 1
If break = 0 ; Loop only as long as break=0
cur$ = Mid( a$, x, 1)
;
;-------- Get out now!!!! --------
;
If cur$ = "\": ; If you want to quit, just
break = 1 ; set break=1
x = 0 ; We set the ForNextLoop2EndVal
Else
ans$ = cur$ + ans$
EndIf
;
EndIf
Next
;
ProcedureReturn ans$
EndProcedure
ans$ = PackDir("c:\Job\BobJob\HarryGuy\Yesterday\BigJob")
MessageRequester("", ans$,0)
PIII450, 256MB Ram, 80GB HD + 6,4 GB, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...
greetz
MrVainSCL! aka Thorsten
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by PB.
> How do you exit early from a For:Next loop?
If would be great if Fred adds Break/Continue commands, but until then
you just set the tested condition to the ending "Next" value. So, to
exit from For a=1 to 10, you would use a=10 where you wanted to break.
PB - Registered PureBasic Coder
> How do you exit early from a For:Next loop?
If would be great if Fred adds Break/Continue commands, but until then
you just set the tested condition to the ending "Next" value. So, to
exit from For a=1 to 10, you would use a=10 where you wanted to break.
Code: Select all
; User enters text up to 999 times or until "end" is entered.
OpenConsole()
For a=1 To 999
Print("Enter text: ")
a$=Input() : PrintN("")
If a$="end" : a=999 : EndIf
Next
PrintN("")
PrintN("You broke out of the For/Next loop!")
Input()
PB - Registered PureBasic Coder
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by PB.
> Its exactly what i do in my example!
Hehehe, sorry -- to be honest I didn't read your example.
> I think a good coder dont need any Goto nor FakeReturn!
)
It's not considered a good programming practice these days to use
Goto/FakeReturn to exit a loop. The functionality is there for
compatibility reasons, but is best avoided in the first place.
PB - Registered PureBasic Coder
> Its exactly what i do in my example!
Hehehe, sorry -- to be honest I didn't read your example.
> I think a good coder dont need any Goto nor FakeReturn!
It's not considered a good programming practice these days to use
Goto/FakeReturn to exit a loop. The functionality is there for
compatibility reasons, but is best avoided in the first place.
PB - Registered PureBasic Coder
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Justin.
If the idea is this:
For a=1 To 999
messagerequester("",str(a),0)
if a=5 : a=999 : endif ;trying to break
messagerequester("","loop complete",0)
Next
messagerequester("","loop exit",0)
That is not a break, 'loop complete' is executed after forcing a=999, the loop is always finished, a break transfers control inmediately to the statement following the next, endselect or whatever. Everything between the break and the next is not executed, the only way is using a goto.
Continue transfers control to the beginning of the loop, in a for/next the index is incremented, in a select/endselect transfers control to the next case.
The break is extensively used in the windows message loop, look at any c code.
If the idea is this:
For a=1 To 999
messagerequester("",str(a),0)
if a=5 : a=999 : endif ;trying to break
messagerequester("","loop complete",0)
Next
messagerequester("","loop exit",0)
That is not a break, 'loop complete' is executed after forcing a=999, the loop is always finished, a break transfers control inmediately to the statement following the next, endselect or whatever. Everything between the break and the next is not executed, the only way is using a goto.
Continue transfers control to the beginning of the loop, in a for/next the index is incremented, in a select/endselect transfers control to the next case.
The break is extensively used in the windows message loop, look at any c code.