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 Code Downloads for this Chapter

The code downloads for this chapter are found at ?isbn=1118311813 on the Download Code tab. The code is in the 311813 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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.