Author Topic: Multiple loop exits  (Read 168 times)

eoredson

  • Hero Member
  • *****
  • Posts: 633
  • I programmed myself into a psychiatric conniption!
    • QB45 Oredson files at Filegate Network
Multiple loop exits
« on: May 17, 2018, 09:48:14 pm »
I was thinking (again) if loop exits, such as:

Code: [Select]
DO
    FOR N=1 TO 10
        IF N=5 THEN EXIT DO
    NEXT
LOOP

Or,

Code: [Select]
FOR N=1 TO 10
    DO : X = X + 1
        IF X=5 THEN EXIT FOR
    LOOP
NEXT

could be done, but what if _CONTINUE had a parameter, such as,

Code: [Select]
D0
    FOR N=1 TO 10
        IF N=5 THEN
            _CONTINUE 2
        ENDIF
    NEXT
LOOP

would exit 2 loops!?

Erik.
« Last Edit: May 17, 2018, 09:56:46 pm by eoredson »

krovit

  • Newbie
  • *
  • Posts: 25
Re: Multiple loop exits
« Reply #1 on: May 17, 2018, 11:33:30 pm »
Hi Erik

I was rushed ... the examples work ... I do not understand what you intend to do in the last case
« Last Edit: May 17, 2018, 11:44:53 pm by krovit »

FellippeHeitor

  • QB64 Partner Site Owner
  • Hero Member
  • *
  • Posts: 1901
  • LET IT = BE
    • QB64.org
Re: Multiple loop exits
« Reply #2 on: May 18, 2018, 12:22:05 am »
What you need is a GOTO.

RhoSigma

  • Sr. Member
  • ****
  • Posts: 380
  • Out of Time !!
Re: Multiple loop exits
« Reply #3 on: May 18, 2018, 12:28:00 am »
_CONTINUE is not for exiting a loop, it's used to immediatly start the next iteration of a loop, ignoring all instructions after the _CONTINUE until the regular loop end.

keybonicplague

  • Sr. Member
  • ****
  • Posts: 275
  • Keepin' it gangsta.
Re: Multiple loop exits
« Reply #4 on: May 18, 2018, 12:00:43 pm »
What you need is a GOTO.

LOL. That is nuts... I dont even know how many times you've told me to delete GOTO statements in my programs...
Never thought I'd see the day that you actually suggested someone use it... :D Hell must have frozen over.
Thank you and remember folks, Jesus saves but George Nelson withdraws!!!

keybonicplague

  • Sr. Member
  • ****
  • Posts: 275
  • Keepin' it gangsta.
Re: Multiple loop exits
« Reply #5 on: May 18, 2018, 12:07:00 pm »
It is possible to exit loops other loops as-is.. You just have to be clever about it.

Code: [Select]
DO
    FOR i = 1 TO 20
        IF i = 17 THEN
            EXIT DO
        END IF
    NEXT i
LOOP
PRINT i

That piece of code will exit both loops when i = 17.
since the for ... next loop is inside the do ... loop, when the do loop is exited the for...next loop is too.

The method works as long as a loop is contained within a different kind of loop, if there is one DO ... LOOP embedded
within another DO ... LOOP then I believe EXIT DO would just exit the innermost loop. Didnt test that though...
Thank you and remember folks, Jesus saves but George Nelson withdraws!!!

FellippeHeitor

  • QB64 Partner Site Owner
  • Hero Member
  • *
  • Posts: 1901
  • LET IT = BE
    • QB64.org
Re: Multiple loop exits
« Reply #6 on: May 18, 2018, 12:08:44 pm »
What you need is a GOTO.

LOL. That is nuts... I dont even know how many times you've told me to delete GOTO statements in my programs...
Never thought I'd see the day that you actually suggested someone use it... :D Hell must have frozen over.

Nah, I'm not that a fighter against gotos, you'll find it in my programs too. The thing is: for certain programming needs, the only answer is a GOTO sometimes. Of course that when there's a more elegant way to do it, I try to choose it.

keybonicplague

  • Sr. Member
  • ****
  • Posts: 275
  • Keepin' it gangsta.
Re: Multiple loop exits
« Reply #7 on: May 18, 2018, 12:11:49 pm »
What you need is a GOTO.

LOL. That is nuts... I dont even know how many times you've told me to delete GOTO statements in my programs...
Never thought I'd see the day that you actually suggested someone use it... :D Hell must have frozen over.

Nah, I'm not that a fighter against gotos, you'll find it in my programs too. The thing is: for certain programming needs, the only answer is a GOTO sometimes. Of course that when there's a more elegant way to do it, I try to choose it.

I know I was just joking... Sometimes a clever use of GOTO is the best solution. I agree 100%.
Thank you and remember folks, Jesus saves but George Nelson withdraws!!!

eoredson

  • Hero Member
  • *****
  • Posts: 633
  • I programmed myself into a psychiatric conniption!
    • QB45 Oredson files at Filegate Network
Re: Multiple loop exits
« Reply #8 on: May 18, 2018, 02:42:30 pm »
I spose a GOTO would do the trick, and yes, I think I mixed up a Continue with a Break..

Erik.

(or something like this):

Code: [Select]
DO
    FOR N=1 TO 10
        IF N=5 THEN
            _BREAK 2
        ENDIF
    NEXT
LOOP

« Last Edit: May 23, 2018, 11:22:51 pm by eoredson »

SMcNeill

  • Moderator
  • Hero Member
  • *****
  • Posts: 6253
Re: Multiple loop exits
« Reply #9 on: May 18, 2018, 03:43:55 pm »

Code: [Select]
D0
    FOR N=1 TO 10
        IF N=5 THEN GOTO ExitPoint1
        ENDIF
    NEXT
LOOP
ExitPoint1:

[/quote]
http://bit.ly/Color32BI -- A set of color CONST for use in 32 bit mode, as a BI library.

eoredson

  • Hero Member
  • *****
  • Posts: 633
  • I programmed myself into a psychiatric conniption!
    • QB45 Oredson files at Filegate Network
Re: Multiple loop exits
« Reply #10 on: May 18, 2018, 05:23:56 pm »
I guess the question is:

If there is a 'C' statement for _continue in qb64
could there be one for _break in qb64?

Erik.

bplus

  • Hero Member
  • *****
  • Posts: 764
  • B = B + _
Re: Multiple loop exits
« Reply #11 on: May 19, 2018, 06:17:33 am »
https://www.programiz.com/c-programming/c-break-continue-statement

It looks to me like Break in C is same as EXIT FOR, EXIT WHILE.... in 64.

And GOTO is the one to go to to leap several loops in a single bound, though I tend to set flags to extract myself out of multiple loops gracefully.

Also I would be more inclined to jump down stream of code flow than a more risky up stream jump.
« Last Edit: May 19, 2018, 06:24:10 am by bplus »
Will you still love me, will you still need me, when I'm (QB) 64?