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 4 Control Structures: Part 1 125<br />

requires that a value be obtained from the command window. The pseudocode statement<br />

Validate that the side is less than or equal <strong>to</strong> 20<br />

can be refined as<br />

If side is less than or equal <strong>to</strong> 20<br />

which explicitly tests whether side is less than or equal <strong>to</strong> 20. If the condition (i.e., side is<br />

less than or equal <strong>to</strong> 20) is true, the first statement in the body of the If is executed. If the<br />

condition is false, the body of the If is not executed. These two control structures are said<br />

<strong>to</strong> be nested—meaning that one is inside the body of the other.<br />

The pseudocode statement<br />

Draw the square<br />

can be implemented by using nested loops <strong>to</strong> draw the square. In this example, it is known<br />

in advance that there are precisely n rows of n * characters each, so counter-controlled repetition<br />

is appropriate. One loop controls the row in which each * is printed. Inside this loop<br />

(i.e., nested within this loop), a second loop prints each individual *. The refinement of the<br />

preceding pseudocode statement is, then,<br />

Set column <strong>to</strong> one<br />

While column is less than or equal <strong>to</strong> side<br />

Print *<br />

Increment column by one<br />

Print a line feed/carriage return<br />

Increment row by one<br />

After column is set <strong>to</strong> one, the inner loop executes <strong>to</strong> completion (i.e., until column<br />

exceeds side). Each iteration of the inner loop prints a single *. A line feed/carriage<br />

return is then printed <strong>to</strong> move the cursor <strong>to</strong> the beginning of the next line, <strong>to</strong> prepare <strong>to</strong><br />

print the next row of the square. Variable row is incremented by one. If the outer loop<br />

condition allows the body of the loop <strong>to</strong> be executed, column is reset <strong>to</strong> one, because we<br />

want the inner loop <strong>to</strong> execute again and print another row of * characters. If column is<br />

not initialized <strong>to</strong> 1 before each iteration of the inner loop, the repetition condition of the<br />

inner loop will fail for all but the first row of output. Variable row is incremented by one.<br />

This process is repeated until the value of row exceeds side at which point the square of<br />

*’s has been printed.<br />

The complete second refinement appears in Fig. 4.19. Notice that blank lines are used<br />

<strong>to</strong> separate the nested control structures for program readability. Also notice that we added<br />

an Else clause that prints a message if the value input for side is <strong>to</strong>o large.<br />

Good <strong>Program</strong>ming Practice 4.10<br />

Too many levels of nesting can make a program difficult <strong>to</strong> understand. If possible, try <strong>to</strong><br />

avoid using more than three levels of nesting. 4.10<br />

The pseudocode now is refined sufficiently for conversion <strong>to</strong> <strong>Visual</strong> <strong>Basic</strong>. The <strong>Visual</strong><br />

<strong>Basic</strong> program and sample executions are shown in Fig. 4.20.

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

Saved successfully!

Ooh no, something went wrong!