19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

■ VARIABLES<br />

In applied mathematics problems, quantities are referred to by names. For instance, consider<br />

the following high school algebra problem. “If a car travels at 50 miles per hour, how far will<br />

it travel in 14 hours? Also, how many hours are required to travel 410 miles?” The solution to<br />

this problem uses the well-known formula<br />

distance = speed × time elapsed<br />

Here’s how this problem would be solved with a computer program.<br />

Private Sub cmdCompute_Click()<br />

picResults.Cls speed = 50<br />

timeElapsed = 14<br />

distance = speed * timeElapsed<br />

picResults.Print distance<br />

distance = 410<br />

timeElapsed = distance / speed<br />

picResults.Print timeElapsed<br />

End Sub<br />

[Run, <strong>and</strong> then click the comm<strong>and</strong> button. The following is displayed in the picture box.]<br />

700<br />

8.2<br />

The third line of the event procedure sets the speed to 50, <strong>and</strong> the fourth line sets the time<br />

elapsed to 14. The fifth line multiplies the value for the speed by the value for the time elapsed<br />

<strong>and</strong> sets the distance to this product. The next line displays the answer to the first question. The<br />

three lines before the End Sub statement answer the second question in a similar manner.<br />

The names speed, timeElapsed, <strong>and</strong> distance, which hold numbers, are referred to as<br />

variables. Consider the variable timeElapsed. In the fourth line, its value was set to 14. In<br />

the eighth line, its value was changed as the result of a computation. On the other h<strong>and</strong>, the<br />

variable speed had the same value, 50, throughout the program.<br />

In general, a variable is a name that is used to refer to an item of data. The value assigned<br />

to the variable may change during the execution of the program. In <strong>Visual</strong> <strong>Basic</strong>, variable<br />

names can be up to 255 characters long, must begin with a letter, <strong>and</strong> can consist only of letters,<br />

digits, <strong>and</strong> underscores. (The shortest variable names consist of a single letter.) <strong>Visual</strong><br />

<strong>Basic</strong> does not distinguish between uppercase <strong>and</strong> lowercase letters used in variable names.<br />

Some examples of variable names are total, numberOfCars, taxRate_1999, <strong>and</strong> n. As a convention,<br />

we write variable names in lowercase letters except for the first letters of additional<br />

words (as in numberOfCars).<br />

If var is a variable <strong>and</strong> num is a constant, then the statement<br />

var = num<br />

assigns the number num to the variable var. (Such a statement is called an assignment statement.)<br />

Actually, the computer sets aside a location in memory with the name var <strong>and</strong> places<br />

the number num in it. The statement<br />

picBox.Print var<br />

Numbers 37

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

Saved successfully!

Ooh no, something went wrong!