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
Solved

How to continue do/while?

Posted on 2009-05-07
16
1,918 Views
Last Modified: 2012-05-06
OK, I give up guessing. How do I continue a loop? I've tried:

While | do while
    continue
    continue while
    next
   continue do
wend | end while | loop

none of these work. What's the right syntax? I'm ready to resort to goTo!
0
Comment
Question by:jmarkfoley
  • 7
  • 6
  • 2
  • +1
16 Comments
 
LVL 29

Expert Comment

by:QPR
ID: 24323205
Sub doTest()
    Dim intCounter As Integer
    Dim intTest As Integer
    intTest = 1
    intCounter = 1
    Do While intTest = 1
        Debug.Print "This is loop number " & intCounter
        If intCounter >= 5 Then
            intTest = 0
        End If
        intCounter = intCounter + 1
    Loop
End Sub

Programming Fundamentals Using VBA
http://www.aspfree.com/c/a/VB.NET/Programming-Fundamentals-Using-VBA/6/
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 24323362
Hi jmarkfoley,

This explains it nicely
http://visualbasic.freetutes.com/learn-vb6/lesson4.html


Good Luck!

Gary
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 24323846
Thanks for the responses. Maybe I'm missing something in your examples and references, but I don't see the answer to my question. Perhaps I wasn't clear: I want to know how to CONTINUE, e.g.

Do While Not EOF(fnum)
    Line Input #fnum, inRec
    If Len(Trim(inRec)) = 0 Then CONTINUE      ' skip to next record
    ' do stuff here
    :
Loop

The above is not valid in VBA ("sub or function not defined"), nor is:
While
    ... continue while
Wend

etc., all the various combinations I've tried as shown in my initial question don't work. So, how do I do a loop continuation in vba?
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
LVL 46

Expert Comment

by:tbsgadi
ID: 24323971
You don't need continue ,that's the default unless I don't understand you
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 24323997
OK, show me how to code the following (notice two CONTINUE statements):

Do While Not EOF(fnum)
    Line Input #fnum, inRec
    If Len(Trim(inRec)) = 0 Then continue      ' skip to next record

     If moreErrors = True Then
        resultsOut inRec
        extraCnt = extraCnt + 1
        continue
    End If
       
    EmployeeId = strtok(inRec, ",", True)
    refNo = strtok(inRec, ",", False)
    AddrStreet = strtok(inRec, ",", False)
    AddrExtra = strtok(inRec, ",", False)
    City = strtok(inRec, ",", False)
    State = strtok(inRec, ",", False)
    zip = strtok(inRec, ",", False)
    zipPlus = strtok(inRec, ",", False)
    taxYear = strtok(inRec, ",", False)
    errorCode = strtok(inRec, ",", False)
    ' etc.
Loop
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 24324015
Where do you have move to the next record/line?
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 24324025
If you were using a recxordset you would have rs.movenext inside the loop
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 24325354
This is just an example! It doesn't matter what the logic is inside the loop. I'm asking how to "continue" within a loop, not how to read records. But to answer your question, my "next record" is the line right under the 'while': Line Input #fnum, inRec

I don't think you understand my fundamental question. There are various looing constructs: While/Wend, Do While/Loop, etc. In most languages there are ways to alter the loop behavior: "break" - terminates the loop before the condition is met, and "continue" resumes the loop from the beginning bypassing the rest of the logic in the loop. The following are logical equivalents:

While <some condition>
   ' do something
  if <condition> continue     ' this should resume with the <some condition> test
  ' do more things
Wend

While <some condition>
  'do something
  if <condition> goTo there
  ' do more things
there:
Wend

So, I am trying to figure out VBA's "continue" syntax. Do you know if VBA even has a "continue"?

0
 
LVL 46

Accepted Solution

by:
tbsgadi earned 250 total points
ID: 24325411
That's what I've been saying..there is no continue! If you want to exit there's exit
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 24326579
> That's what I've been saying..there is no continue!

Really? That seems like a fundamental language component. Hmm. So, I either have to put everything inside one big IF:

While <some condition>
   ' do something
  if !<condition> then
      ' do more things
  end if
Wend

or use a goto as in my example above?
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 24326651
Depending on what you want to do, you might want to use "Do until" or For...
0
 
LVL 29

Assisted Solution

by:QPR
QPR earned 250 total points
ID: 24330190
there is no continue because you have already specified that you want to "do" while a certain condition is true.
If you want to walk to the shop whuile it is not raining you don't need to continually tell yourself to walk... you just do it until you get there (or it starts raining at which point you exit).

Not trying to be smart, just trying to use an analogy to explain the do loop
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 24347433
Anything else?
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 24383406
QPR - I understand the analogy, I've been programming for 30 years and have taught various languages. Most languages, include VB.net [http://msdn.microsoft.com/en-us/library/801hyx6f(VS.80).aspx] permit one to alter loop interation via break (one gets an emergency call and has to interrupt the shopping prematurely and go home), or some condition arises that makes you want to bypass the rest of the logic in the loop (skip the current shop and move on to the next one). For example, a do or while or for loop that reads records from a file and processes them. If, immediately after reading a record one determines that it is a comment or blank, a 'continue' lets you skip to the next record. Possible programming contstructs are:

1. continue
Do While Not EOF(fnum)
    Line Input #fnum, inRec
    If Len(Trim(inRec)) = 0 Then continue      'blank record, skip to next record
    ' do logic on good records
Loop

2. if
Do While Not EOF(fnum)
    Line Input #fnum, inRec
    If Len(Trim(inRec) > 0 Then
        ' do logic on good records
    End If
Loop

3. goto
Do While Not EOF(fnum)
     Line Input #fnum, inRec
    If Len(Trim(inRec)) = 0 Then goTo nextRec   'blank record, skip to next record
    ' do logic on good records
nextRec:
Loop

#1 is preferable over #2 if the 'do logic on good records' bit is fairly extensive; plus there may be other continuation conditions along the way (errors in the input rec, relational data not found, etc.) which can lead to a rat's nest of nested If's. Apparently VBA does not have a 'continue' loop flow alteration command, so we are left with options #2 or #3.
0
 
LVL 1

Author Closing Comment

by:jmarkfoley
ID: 31578884
basically the answer is "can't do it", so I'll split the points between you for your efforts. Thanks.
0
 
LVL 1

Expert Comment

by:chillguy
ID: 27662080
Jmarkfoley's 3 methods have been tested and do work. Sadly, only VB.NET supports the continue loop statement. Goodluck!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sum in Split Form 17 27
Signature required on a report 5 32
Database keeps telling me Recordset Locked 10 21
Access Creating a report using a date range 11 10
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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