Skipping the rest of an iteration with the CONTINUE Keyword

Navigation:  Appendices > Macro Language > Instructions > Looping Instructions >

Skipping the rest of an iteration with the CONTINUE Keyword

Previous pageReturn to chapter overviewNext page

The continue command is used inside either a for or a while loop. This commands allows you to skip the rest of a loop block and start the next iteration immediately.

 

The continue command is used less often than the break command because it can often be replaced by an if statement which makes code more legible and should be prefered, if convenient. Consider the following example, which we will write in pseudo-code to avoid getting mired in a lot of code lines :

 

// 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

 

Note how, in the above, we have turned the condition upside-down. In that version, if the first name is not "not blank" (ie. it is blank), then nothing happens. 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 is it politically correct to use a continue command ? The answer is : whenever avoiding the continue command makes code less understandable than using it. In the above example, there was only one condition that caused us to call continue, and so it was easy to replace it by a simple IF. If however there are many conditions, then using an if rapidly becomes awkward.

 

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 determinig 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.

 

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. This digression concluded the section on the Instructions block and should have illustrated that there is often no single best way to code a solution, and that there is often a trade-off between legibility and efficiency.


Topic 108199 updated on 01-Nov-2016.
Topic URL: http://www.qppstudio.net/webhelp/index.html?skippingtherestofaniterationwi.htm