Can't "goto" a label within a "While" loop

Please report bugs

Can't "goto" a label within a "While" loop

Postby bobr » Wed Sep 28, 2011 12:14 pm

I need the equivalent of the 'C' "continue" function so that my code will perform the next iteration of a loop. The only clean way I can think of doing this is to use a "goto" with a label just before the "End While".
Sadly, this doesn't appear possible.

Looking at a tiny snippet of my code

while Next_Command() <> Null
if (instr(Strng, "COMMENT") <> 0) then
FieldPtr = StartAt
goto label1
end if
.
.
.
.
etc.

label1:
end while

When I try to build this, I get the error:
parser error: Label1 already declared (label) at some place in line 194 near pos 1

If the label is outside the loop:
.
.
end while
label1:

there is no error BUT, this rather defeats what I'm trying to do.
So, there is either a bug in KBasic, or this is an invalid structure. As this is allowed in VBasic I suspect that this is a bug in KBasic.

Whatever it is, does anyone have a suggested alternative approach to the lack of "continue"? The trouble is that this is a very big function and if I try to use conditionals it will become VERY difficult to follow the code!
bobr
 
Posts: 31
Joined: Fri Mar 18, 2011 10:30 pm

Re: Can't "goto" a label within a "While" loop

Postby Slowdown » Wed Sep 28, 2011 1:50 pm

Hi bobr,

Made a small test this works,
Code: Select all
Dim A As Integer

Private Sub Form_OnOpen()
  A = 1
End Sub

Private Sub CommandButton1_OnEvent()
  While A > 0
    A = A +1
    If A = 100 Then
      goto Label1
    End If
/*
do some other coding here
*/   
  Wend
Label1: 
  Print A
End Sub
Regards
Slowdown for now i'm back
Slowdown
 
Posts: 347
Joined: Sat May 02, 2009 6:48 pm
Location: Netherlands

Re: Can't "goto" a label within a "While" loop

Postby bobr » Wed Sep 28, 2011 5:39 pm

:D
Thanks Slowdown. That's a lot better. I hadn't thought of using "wend". I wonder what the difference is? Mind you, your example wasn't quite what i was trying to do. It's the label WITHIN the "while" that didn't work. However, your suggestion of "wend" does the trick.


Dim A As Integer

Private Sub Form_OnOpen()
A = 1
End Sub

Private Sub CommandButton1_OnEvent()
While A > 0
A = A +1
If A = 100 Then
goto Label1
End If
/*
do some other coding here
*/

Label1:
Wend

Print A
End Sub


Your little example worked equally well with "end while"!
bobr
 
Posts: 31
Joined: Fri Mar 18, 2011 10:30 pm

Re: Can't "goto" a label within a "While" loop

Postby Henning » Wed Sep 28, 2011 7:33 pm

Hi,

I would prefer to do it like this. Goto should not be needed. ;)

Code: Select all
Dim A As Integer

Private Sub Form_OnOpen()
A = 1
End Sub

Private Sub CommandButton1_OnEvent()
While A > 0
  A = A +1
  If A <> 100 Then

    /*
    do some other coding here
    */

  End If
Wend

Print A
End Sub


/Henning
Henning
 
Posts: 262
Joined: Mon Feb 09, 2009 12:03 am
Location: Sweden

Re: Can't "goto" a label within a "While" loop

Postby Henning » Wed Sep 28, 2011 7:50 pm

@Slowdown,

If I had written the compiler I would have raised a big red ERROR on a Goto outside a loop, bypassing any stack cleanup!

/Henning
Henning
 
Posts: 262
Joined: Mon Feb 09, 2009 12:03 am
Location: Sweden

Re: Can't "goto" a label within a "While" loop

Postby bobr » Wed Sep 28, 2011 9:49 pm

Hi Henning

I heartily concur with your sentiment about not jumping outside a contained loop - very nasty when it comes to memory cleanup. However, this was not what I was trying to do. I was jumping within the loop but the compiler wouldn't let me when I used "end while". Fortunately it works with "wend".

What I didn't add, when I first raised this as a bug, was that the "goto" is from within a "select". So, although conditionals would possibly be the preferred approach, it wouldn't be possible to achieve the desired result from within a select (unless I sub routined the select, which, come to think of it, wouldn't be a bad idea!), and anyway, as I also said it's a very big function and would be difficult to follow the logic were I to use conditionals.

I suppose, in retrospect, it would be worth raising a bug on the fact that "end while" DOES allow you to jump outside the loop!
bobr
 
Posts: 31
Joined: Fri Mar 18, 2011 10:30 pm

Re: Can't "goto" a label within a "While" loop

Postby Henning » Thu Sep 29, 2011 12:00 am

@bobr,

The structure of the While...Wend wasn't clear to me, just saw the If A = 100... ;)

The 'jump out of a loop' was directed to Slowdown... :o

The "normal" loops in Basic are: For...Next, Do ... Loop While condition, While condition ... Wend.

I have many times whished there were a Do While thiscondition ... Loop Until thatcondition, two possible exit conditions.

/Henning
Henning
 
Posts: 262
Joined: Mon Feb 09, 2009 12:03 am
Location: Sweden

Re: Can't "goto" a label within a "While" loop

Postby berndnoetscher » Thu Sep 29, 2011 9:14 am

Hi

the keyword/command

Iterate

is intended for that.
berndnoetscher
Site Admin
 
Posts: 1059
Joined: Tue Sep 25, 2007 9:37 am

Re: Can't "goto" a label within a "While" loop

Postby Slowdown » Thu Sep 29, 2011 9:48 am

@Henning,
if I had written the compiler I would have raised a big red ERROR on a Goto outside a loop, bypassing any stack cleanup!

I know, don't use Goto in my codes, but what about the Exit For and the Exit Sub i expected there was an Exit While.
The 'jump out of a loop' was directed to Slowdown...

My mistake, saw the code posted by bobr > started KB and typed my simple code, typo :oops:
In my opinion the error wasn't raised by the Goto and in fact that is what i demonstrated, so the error was coming from elsewhere.
When using VB6/KB/Q7B there is no need for using Goto and/or Labels and neither of them i ever used ;)
But the main thing : problem solved.
Regards
Slowdown for now i'm back
Slowdown
 
Posts: 347
Joined: Sat May 02, 2009 6:48 pm
Location: Netherlands


Return to Bug-Reports

cron