SUMMARY

Control statements form the heart of any program. Decision statements determine what commands are executed, and looping statements determine how many times they are executed.

Single-line and multiline If Then statements, as well as Select Case statements, are the most commonly used decision statements. IIf and Choose statements are often more confusing and sometimes slower, so usually you should use If Then and Select Case statements instead. Under some specific circumstances, however, IIf and Choose may make your code more readable. Use your judgment and pick the method that makes the most sense in your application.

For Next, For Each, and Do Loop are the most common looping statements. Some container classes also support enumerators that let you step through the items in the container. An enumerator can be more natural than a For Each loop under some circumstances.

A While End loop is equivalent to Do While Loop. You can use whichever you think makes more sense, although you might want to use Do While because it is more consistent with the other forms of Do Loop.

Using the control statements described in this chapter, you can build extremely complex and powerful applications. In fact, you can build applications that are so complex that it is difficult to ensure that they work correctly. Even a relatively simple application sometimes encounters errors. Chapter 18, “Error Handling,” explains how you can protect an application from unexpected errors and let it take action ...

Get Visual Basic 2012 Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.