Chapter 4

Controlling the Flow

What You Will Learn in this Chapter

  • How to use the If statement
  • How to use Select Case
  • How to use For loops and Do loops

Wrox.com Code Downloads for this Chapter

The wrox.com code downloads for this chapter are found at www.wrox.com/remtitle.cgi ?isbn=1118311813 on the Download Code tab. The code is in the 311813 C04.zip download and individually named according to the names given throughout the chapter.

In Chapter 3, you learned about algorithms and their role in programming. In this chapter, you will look at how you can control the flow through your algorithms so that you can make decisions like “If X is the case, go and do A; otherwise do B.” This ability to make decisions is known as branching. You'll also see how you can repeat a section of code (a process known as looping) a specified number of times, or while a certain condition applies.

Making Decisions

Algorithms often include decisions. It's this decision-making ability that makes computers do what they do so well. When you're writing code, you make two kinds of decisions. The first kind is used to find out what part of an algorithm you're currently working on or to cope with problems. For example, imagine that you have a list of 10 people and need to write a piece of code to send an e-mail to each of them. To do this, after sending each e-mail, you ask, “Have I finished?” If so, you quit the algorithm; otherwise, you get the next person in the list. As another example, you might need ...

Get Beginning Visual Basic 2012 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.