30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5 Control Structures: Part 2 145<br />

Henri Bergson<br />

Outline<br />

5.1 Introduction<br />

5.2 Essentials of Counter-Controlled Repetition<br />

5.3 For/Next Repetition Structure<br />

5.4 Examples Using the For/Next Structure<br />

5.5 Select Case Multiple-Selection Structure<br />

5.6 Do/Loop While Repetition Structure<br />

5.7 Do/Loop Until Repetition Structure<br />

5.8 Using the Exit Keyword in a Repetition Structure<br />

5.9 Logical Opera<strong>to</strong>rs<br />

5.10 Structured <strong>Program</strong>ming Summary<br />

Summary Terminology Self-Review Exercises Answers <strong>to</strong> Self-Review Exercises Exercises<br />

5.1 Introduction<br />

Before writing a program <strong>to</strong> solve a particular problem, it is essential <strong>to</strong> have a thorough<br />

understanding of the problem and a carefully planned approach <strong>to</strong> solving it. It is equally<br />

essential <strong>to</strong> understand the types of building blocks available and <strong>to</strong> employ proven program-construction<br />

principles. In this chapter, we discuss these issues in conjunction with<br />

our presentation of the theory and principles of structured programming. The techniques<br />

we explore are applicable <strong>to</strong> most high-level languages, including <strong>Visual</strong> <strong>Basic</strong>. In Chapter<br />

8, Object-Based <strong>Program</strong>ming, we show how the control structures we present in this<br />

chapter are useful in the construction and manipulation of objects.<br />

5.2 Essentials of Counter-Controlled Repetition<br />

In the last chapter, we introduced the concept of counter-controlled repetition. In this section,<br />

we formalize the elements needed in counter-controlled repetition, namely:<br />

1. The name of a control variable (or loop counter) that is used <strong>to</strong> determine whether<br />

the loop continues <strong>to</strong> iterate.<br />

2. The initial value of the control variable.<br />

3. The increment (or decrement) by which the control variable is modified during<br />

each iteration of the loop, or each time the loop is performed).<br />

4. The condition that tests for the final value of the control variable (i.e., whether<br />

looping should continue).<br />

The example in Fig. 5.1 uses the four elements of counter-controlled repetition <strong>to</strong> display<br />

the even digits from 2–10.<br />

The declaration in line 8 names the control variable (counter), indicates that it is of<br />

data type Integer, reserves space for it in memory and sets it <strong>to</strong> an initial value of 2. This

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!