How do I make use of 2 loop statements for 1 do?

Iketh

Active member
Joined
Nov 23, 2009
Messages
40
Programming Experience
Beginner
I need an additional loop argument within an If statement that resides in a very large do...loop block. It will speed the application considerably if I can have the do...loop block loop prematurely.

I already have the remaining code after the additional loop argument in a large if...elseif block to achieve the result, but making it simply loop instead of going through this would speed the application.
 
Use a boolean to check if the loop needs to execute, if so loop else do nothing and it passes this loop code. Psuedo code:
VB.NET:
Do 
... 
   If useMyloop Then
       For...
   End If
...
Loop
 
Might not be a bad idea to look at refactoring the whole process to cut out the short circuiting of the loop, but this should Do for you :D (pun intended).

VB.NET:
Do While True

            'initial stuff

            If UseMyLoop Then
                'Loop short circuit
                Continue Do

            End If

            'other stuff
        Loop
 
Here is the basis of my code:

VB.NET:
Do
   a += 1
   if a mod 10 = 0 then
      <bunch of code>
      if cancelLoopEarly then a += 9
   end if
   if ....
   elseif ...
   <several more elseifs>
   end if
   <more code>
Loop

I want it to loop when it adds 9 to the variable "a" instead of running through the rest of the code needlessly.
 
Exit the loop:
VB.NET:
Do
   a += 1
   if a mod 10 = 0 then
      <bunch of code>
      if cancelLoopEarly then 
         a += 9
         exit Do
      end if
   if ....
   elseif ...
   <several more elseifs>
   end if
   <more code>
Loop
 
Hi
I am not sure if you solved your problem.

If you want to have "Bad code" but "Easy to work" code you can write
VB.NET:
Goto MyLine
as command where you want to jump to the line where you write
VB.NET:
MyLine:

Example 1 (Without GOTO):
VB.NET:
Dim CancelLoop As Boolean = False
Dim i As Integer = 0

Do
  i += 1
  If CancelLoop = True Then Exit Do
  If i mod 10 = 0 then
    <buchofcode>
  end if
  
  <Some more codes>
  <Some more codes>
  <Some more codes>
Loop 'When Cancel is True: it jumps here

<Some more codes>


Example 2 (With Goto):
VB.NET:
Dim CancelLoop As Boolean = False
Dim i As Integer = 0

Do
  i += 1

  If i mod 10 = 0 then
    If CancelLoop = True Then Goto MyLine
    <buchofcode>
  end if
  
  <Some more codes>
  MyLine: 'When Cancel is True: it jumps here
  <Some more codes>
  <Some more codes>
  If CancelLoop = True Then Exit Do
Loop 

<Some more codes>

Regards
Timo
 
From what the op said he does not want to exit the Do Loop he just wants to continue processing it without processing it to the loop. Continue Do does this.
 
Hey outstanding replies! I wasn't aware goto was still supported lol, but continue do was exactly what I was looking for, thank you very much!
 
Back
Top