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.

136 Control Structures: Part 1 Chapter 4<br />

Syntax errors are caught by the compiler. Logic errors affect the program only at execution time.<br />

Fatal logic errors cause a program <strong>to</strong> fail and terminate prematurely. Nonfatal logic errors do not<br />

terminate a program’s execution but cause the program <strong>to</strong> produce incorrect results.<br />

Nested If/Then/Else structures test for multiple conditions by placing If/Then/Else structures<br />

inside other If/Then/Else structures.<br />

The While and Do While/Loop repetition structures allow the programmer <strong>to</strong> specify that an<br />

action is <strong>to</strong> be repeated while a specific condition remains true.<br />

Eventually, the condition in a While or Do While/Loop structure becomes false. At this point,<br />

the repetition terminates, and the first statement after the repetition structure executes.<br />

Failure <strong>to</strong> provide in the body of a While or Do While/Loop structure an action that eventually<br />

causes the condition <strong>to</strong> become false is a logic error. Normally, such a repetition structure never<br />

terminates, resulting in an error called an “infinite loop.”<br />

Statements in the body of a Do Until/Loop are executed repeatedly as long as the loop-continuation<br />

test evaluates <strong>to</strong> false.<br />

Failure <strong>to</strong> provide the body of a Do Until/Loop structure with an action that eventually causes<br />

the condition in the Do Until/Loop <strong>to</strong> become true creates an infinite loop.<br />

<strong>Visual</strong> <strong>Basic</strong> provides the assignment opera<strong>to</strong>rs +=, -=, *=, /=, \=, ^= and &= for abbreviating<br />

assignment statements.<br />

In counter-controlled repetition, a counter is used <strong>to</strong> repeat a set of statements a certain number of<br />

times. Counter-controlled repetition is also called definite repetition because the number of repetitions<br />

is known before the loop begins executing.<br />

A <strong>to</strong>tal is a variable used <strong>to</strong> calculate the sum of a series of values.<br />

It is important that variables used as <strong>to</strong>tals and counters have appropriate initial values before they<br />

are used. Counters usually are initialized <strong>to</strong> one. Totals generally are initialized <strong>to</strong> zero.<br />

Data types Double and Single s<strong>to</strong>re floating-point numbers. Data type Double requires more<br />

memory <strong>to</strong> s<strong>to</strong>re a floating-point value, but is more accurate and generally more efficient than type<br />

Single.<br />

In sentinel-controlled repetition, the number of repetitions is not known before the loop begins its<br />

execution. Sentinel-controlled repetition uses a sentinel value (also called a signal value, dummy<br />

value or flag value) <strong>to</strong> terminate repetition.<br />

We approach programming problems with <strong>to</strong>p-down, stepwise refinement, a technique essential<br />

<strong>to</strong> the development of well-structured algorithms.<br />

The <strong>to</strong>p is a single statement that conveys the overall function of the program. As such, the <strong>to</strong>p is<br />

a complete representation of a program.<br />

Through the process of refinement, we divide the <strong>to</strong>p in<strong>to</strong> a series of smaller tasks that are listed<br />

in the order in which they must be performed. Each refinement, including the <strong>to</strong>p, is a complete<br />

specification of the algorithm; only the level of detail in each refinement varies.<br />

Many algorithms can be divided logically in<strong>to</strong> three phases: An initialization phase that initializes<br />

the program variables, a processing phase that inputs data values and adjusts program variables<br />

accordingly, and a termination phase that calculates and prints the results.<br />

The programmer terminates the <strong>to</strong>p-down, stepwise refinement process when the pseudocode algorithm<br />

is specified in sufficient detail for the pseudocode <strong>to</strong> be converted <strong>to</strong> a <strong>Visual</strong> <strong>Basic</strong> program.<br />

The implementation of the <strong>Visual</strong> <strong>Basic</strong> program then occurs in a normal, straightforward<br />

manner.<br />

The constants vbCrLf and vbTab represent the carriage return/linefeed character and the tab<br />

character, respectively.

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

Saved successfully!

Ooh no, something went wrong!