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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Again, note that (besides small circles and flowlines) the flowchart contains only rectangle<br />

and diamond symbols. Imagine, as we did in the previous chapter, that the programmer<br />

has access <strong>to</strong> a deep bin of empty structures. This time, the bin contains Select<br />

Case structures, and the programmer can stack and nest as many as are necessary with<br />

other control structures <strong>to</strong> form a structured implementation of an algorithm’s flow of control.<br />

The programmer fills the rectangles and diamonds with actions and decisions appropriate<br />

<strong>to</strong> the algorithm. Although nested control structures are common, it is rare <strong>to</strong> find<br />

nested Select Case structures in a program.<br />

In Chapter 10, Object-Oriented <strong>Program</strong>ming: Part 2, we present a more elegant<br />

method of implementing multiple selection logic. We use a technique called polymorphism<br />

<strong>to</strong> create programs that are often clearer, more manageable, and easier <strong>to</strong> extend than programs<br />

that use Select Case logic.<br />

5.6 Do/Loop While Repetition Structure<br />

The Do/Loop While repetition structure is similar <strong>to</strong> the While structure and Do<br />

While/Loop structure. In the While and Do While/Loop structures, the loop-continuation<br />

condition is tested at the beginning of the loop, before the body of the loop is performed.<br />

The Do/Loop While structure tests the loop-continuation condition after the loop<br />

body is performed. Therefore, in a Do/Loop While structure, the loop body is always executed<br />

at least once. When a Do/Loop While structure terminates, execution continues<br />

with the statement after the Loop While clause. The program in Fig. 5.12 uses a Do/Loop<br />

While structure <strong>to</strong> output the values 1–5.<br />

Testing and Debugging Tip 5.4<br />

Infinite loops occur when the loop-continuation condition in a While, Do While/Loop or<br />

Do/Loop While structure never becomes false. 5.4<br />

1 ' Fig. 5.12: DoWhile.vb<br />

2 ' Demonstrating the Do/Loop While repetition structure.<br />

3<br />

4 Module modDoWhile<br />

5<br />

6 Sub Main()<br />

7 Dim counter As Integer = 1<br />

8<br />

9 ' print values 1 <strong>to</strong> 5<br />

10 Do<br />

11 Console.Write(counter & " ")<br />

12 counter += 1<br />

13 Loop While counter

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

Saved successfully!

Ooh no, something went wrong!