Problem using Select Case with nested loops using

I am working on a project that requires some looping predetermined number of times.  Within the For Next Loop I am trying to use Select Case to read string values until a certain condition is met.  Below is my code and hopefully an understandable description of what my problem is:

To simply the example I will explain what happens within the do while loop...... A text file is broken down line by line and then each line is broken into “segments” using the split function.  Using Select Case I am reading the first “Segment” of line to determine if a specific value is contained in the “segment”

xx = 10  Value of xx is determine earlier in the code

For k = 1 To xx

          xDone = False

               Do While xDone = False

                    Select Case xSeg(0)
                          Case Is = “ABC”
                                ‘Then do something and continue on
                          Case Is = “DEF”
                                ‘Then do something and continue on
                         Case is = “XYZ”
                                      xDone = ‘True’  Indicate condition was met
                                     'Condition met...move on
                         End Select

                       Code --- > ‘If xDone is still false then move to next line of data and break into segments and start process again,


               When the ‘xDone’ condition has been met and xDone = True the Do While Loop should exit back into the for next loop at the .    

Next k

The problem I am having is that this process should run ten times as indicated in the for (For k = 1 To xx ) next but it only will run a maximum of three times.    Sometimes the data that is being process requires that the For Next should only loop once or twice or sometimes more but it will only loop a maximum of three times.  

It has something to do with the Select Case because if I remove that whole section of the code everything works fine.  

Im not a expert and I cant figure it out on my own.  Any help would be appreciated.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sounds like you are modifying the value of k within the for next block as this is the only reason that it would not be iterated 10 times.

You could put a debug line at the top and bottom of the for loop to print the current value of k

For k = 1 To xx



Next k

Open in new window

this will tell you if the value is being modified during the execution of the loop
Mike_StevensAuthor Commented:
The value of K is being modified as it should and gets to three and that's when for next look gets exited.  It should go to 10
Jacques Bourgeois (James Burger)PresidentCommented:
First, a little trick. Instead of using a variable to for an exit out of the loop, simply use the following:

Do While True
   Exit Do 'When you want to exit.

Now, for your question.

First thing, dummy at first sight, but it has to be asked. Are you sure that xx=10. Have you tried by looping 1 to 10 instead of 1 to xx.

When you do not show the real code, you often omit the code that is responsible for your problem.

This is the case here, we do not see what happens in the loop, and the problem is there somewhere. Then do something and continue on hides the cause.

Could be that you play with k by mistake as suggested by Chloes. If k is not a local variable, it might not be modified in the Select block, but could be by some method that is called inside of the block.

For such a type of loop, it is usually better to do:

For k As Integer = 0 to xx


That way you are sure that your loop counter cannot be modified somewhere else by mistake.

There could be an Exit For or an Exit Sub or something else.

Without seeing the real code, anything is but a guess.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The value of K is being modified by what, the for next will modify (increment it anyway). As James has said there are better tools to exit a loop than by modifying the loop counter within the loop.

What is the code that is modifying the value of k?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.