Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Loop a Do While in an IF

Posted on 2010-09-16
19
Medium Priority
?
414 Views
Last Modified: 2012-05-10
I have a DO WHILE statement and in that statement there is an IF ststement, is it possible to loop the origional DO ststement in the IF ststement
0
Comment
Question by:ict-torquilclark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
  • +3
19 Comments
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 33689421
do u mean that the DO WHILE should be inside the IF statement?can u post the code?
0
 
LVL 1

Author Comment

by:ict-torquilclark
ID: 33689438
Sorry - i didnt explain very well

I I know the code doesnt achieve anything but it just came up with it as an example of what i am trying to do.

in the code x should never = 4

when i do it i get the error

"loop without DO"
x = 1
Do While x < 10

IF x = 3 THEN
    x = x + 2  
    loop
END IF

x = x + 1
loop

Open in new window

0
 
LVL 7

Expert Comment

by:klakkas
ID: 33689446
I am not sure I undestand, but here is something that might solve your problem.

Do while Finished = False
       if I_Want_To_Exit=true then
              Exit Do
       End If
       If I_Want_to_Move_to_Next = true then
              Continue Do
       End If
       <Your Normal Code>
Loop

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 7

Expert Comment

by:klakkas
ID: 33689459
After reading your second post, "Continue Do" is your solution.

This command will move the code to the "Do" statement.
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 33689460
try this:

x = 1
Do While x < 10

IF x = 3 THEN
    x = x + 2
ELSE
   x = x + 1
END IF

loop
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 33689539
How about the following ... I have placed a stop itf it hits 4 ... which it doesn't.

You can change the line:
If x = 4 Then Stop

to for example 3 to see it stop since all other values are processed

Chris
    x = 1
    Do While x < 10
        If x = 3 Then
            x = x + 1
        End If
        x = x + 1
        If x = 4 Then Stop
    Loop

Open in new window

0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 33689562
SLightly modified to support a demo output

Chris
    x = 0
    Do While x < 10
        x = x + 1
        If x = 4 Then
            x = x + 1
        End If
        If x = 4 Then
            Stop
        Else
            Debug.Print x
        End If
    Loop

Open in new window

0
 
LVL 1

Author Comment

by:ict-torquilclark
ID: 33689884
basically i need it to stop the current loop and continue on with the next loop if the value of the if is true
0
 
LVL 7

Expert Comment

by:klakkas
ID: 33689896
As I said above my friend, the command

Continue Do

will do exacty that.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 33689926
Removing other aspects then .. see the comment this is where you your bit ... and it isn't triggered if x = 4

Chris
    Do While x < 10
        x = x + 1
        If x <> 4 Then
            ' do your stuff
        End If
    Loop

Open in new window

0
 
LVL 1

Author Comment

by:ict-torquilclark
ID: 33690014
I have tried Continue do and it is not recognised as a command

i am using vb6...?
0
 
LVL 7

Accepted Solution

by:
klakkas earned 2000 total points
ID: 33690079
Since you are using vb6 (I thought it was .NET), the only way to do this is with the GOTO statement.

Using your example:

x = 1
Do While x < 10
     
      IF x = 3 THEN
          x = x + 2  
          GOTO ContinueLoop
      END IF

      x = x + 1

ContinueLoop:
loop
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 33690123
The only way??? - not so, see my earlier post.

Chris
0
 
LVL 7

Expert Comment

by:klakkas
ID: 33690265
chris_bottomley, yes there are several things you can do to work around the need for a continue statement.

Still, if you really need the "Continue Do" functionality, the way to go is with GOTO.
0
 
LVL 14

Expert Comment

by:VBClassicGuy
ID: 33691314
Your original code will work with a slight modification:
x = 1
Do While x < 10
   IF x = 3 THEN
      x = x + 2
   Else
      x = x + 1
   END IF
Loop  
Simple...
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 33696795
@VBClassicGuy

like my suggestion...
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 33699132
For my education why did you select the option:

Do While x < 10
            ' do your stuff
      IF x = 3 THEN
          x = x + 2  
          GOTO ContinueLoop
      END IF

      x = x + 1

ContinueLoop:
loop

It seems much more complex than:

    Do While x < 10
        x = x + 1
        If x <> 4 Then
            ' do your stuff
        End If
    Loop
 
i.e. 5 lines and a jump label as compared to two lines without.  (Jumps are genrally considered bad practice though sometimes are unavoidable.

ALso note my method doesn't care what value you start with ... the accepted solution will fall over when starting at 4 - not critical if you always intend to start on 1 but nonetheless more risky.

Chris
0
 
LVL 7

Expert Comment

by:klakkas
ID: 33699213
My friends, I agree that the goto statement in VB6 should only be used for Error Handling and that the correct programming technique is to avoid jumping around the code and creating spagetti code.

BUT, sometimes it is the easiest way around a problem. In the example that ict-torquilclark posted, it wouldn't make any sense. But in a loop where there are several exit positions spread around the loop, maybe it is the correct way to go.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 33804451
IMHO: The selected comment is a poor solution.

Have a look at this relevant article.
http://www.experts-exchange.com/A_2755.html

aikimark
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question