Please enable JavaScript to view this site.

 

Navigation: Macros > Macro Language > Instructions > Looping

Skipping the Rest of an Iteration with the CONTINUE Keyword

Scroll Prev Up Next More

The continue instruction is used inside either a for or a while loop, to skip the rest of a loop block, and start the next iteration immediately.

 

The use of continue command can often be replaced by an if branching statement which may make the code more legible, as in the following example, written in pseudo-code.

// get a list of users' names 
// and append Mr.or Mrs. depending on the first name
while there are more users
   get first and last names
   if FirstNane is blank
      continue // no use wasting our time below
   endif
   if FirstName is feminine
      use Mrs.
   else
      use Mr.
   endif
endwhile

The above is a valid example of the use of continue. It uses it to avoid doing work in the rest of the loop which we know will be wasted (if we have no first name, we will not be able to tell if the person is male or female, and presumably the work involved in deciding the gender of a name is complex).

 

However, as mentioned above, in some cases an if branch may work the same and be more legible. We can rewrite the above example as :

while there are more users
   get first and last names
   if FirstNane is not blank
      if FirstName is feminine
         // use Mrs.
      else
         // use Mr.
      endif
   endif
endwhile

In the above, we have turned the condition upside-down, and we check if the first name is blank for anything to happen. This example works just as well but is more legible because the flow of execution is clear.

 

The problem with continue is that execution is suddenly taken out of its logical path. This may not seem to be a problem here in a small code sample, but when you have a loop of over 30 lines with nested if statements it is easy to overlook a continue statement tucked away somewhere.

 

So, when should we use a continue command ? The answer is a matter of taste, but generally if the code becomes more complex or harder to understand, then it should be replaced by and if branch at the beginning of the loop.

 

Consider the above example where the determination of the gender of a first name is so complex that we want to skip it if we know that the result will be inconclusive. So far we have tested for a blank first name, which clearly would give no gender. Suppose we now want to refine this by checking for first names that are both male and female (we'll have to use French names for this). If we try to add these conditions using if statements the result soon becomes awkward.

while there are more users
   // get first and last names
   if FirstNane is not blank
      if FirstNane is not 'Dominique'
         if FirstNane is not 'Claude'
            if FirstName is feminine
               // use Mrs.
            else
               // use Mr.
            endif
         endif
      endif
   endif
endwhile

Note that the above only checks for 2 names. If we had a list of 50, you can imagine what the code would look like. In contrast, in this case, using continue leads to more legible code.

while there are more users
   get first and last names
   // get rid of potential time-wasters
   if FirstNane is blank
      continue
   endif
   if FirstNane is 'Dominique'
      continue
   endif
   if FirstNane is 'Claude'
      continue
   endif
   // once we have eliminated time-wasters …
   if FirstName is feminine
      // use Mrs.
   else
      // use Mr.
   endif
endwhile

The above code is not really shorter, but it has the merit of clearly showing what is going on; first check for any condition that would make determining the gender a waste of time, and then determine the gender.

 

As an aside, note that the above could still be rewritten using an if branch, as follows.

while there are more users
   get first and last names
   // get rid of potential time-wasters
   bSkipName = false
   bSkipName = bSkipName or (FirstName == '')
   bSkipName = bSkipName or (FirstName == 'Dominique')
   bSkipName = bSkipName or (FirstName == 'Claude')
   // once we have eliminated time-wasters …
   if not bSkipName
      if FirstName is feminine
         // use Mrs.
      else
         // use Mr.
      endif
   endif
endwhile

While the above is certainly more compact and easier to read, it does have the disadvantage that all time-wasters must be checked before reaching the IF branch, even if the FirstName was blank.

 

See also: for loops, while loops, break interruption.

 


Topic 108199, last updated on 18-Apr-2020