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

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

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

38 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

looks into this memory location for the value of the variable <strong>and</strong> displays the value in the picture<br />

box.<br />

A combination of constants, variables, <strong>and</strong> arithmetic operations that can be evaluated<br />

to yield a number is called a numeric expression. Expressions are evaluated by replacing<br />

each variable by its value <strong>and</strong> carrying out the arithmetic. Some examples of expressions are<br />

2 * distance + 7, n + 1, <strong>and</strong> (a + b) / 3.<br />

EXAMPLE 3<br />

The following program displays the value of an expression.<br />

Private Sub cmdCompute_Click()<br />

picResults.Cls<br />

a = 5<br />

b = 4<br />

picResults.Print a * (2 + b)<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 />

30<br />

If var is a variable, then the statement<br />

var = expression<br />

first evaluates the expression on the right <strong>and</strong> then assigns its value to the variable. For instance,<br />

the event procedure in Example 3 can be written as<br />

Private Sub cmdCompute_Click()<br />

picResults.Cls<br />

a = 5<br />

b = 4<br />

c = a * (2 + b)<br />

picResults.Print c<br />

End Sub<br />

The expression a * (2 + b) is evaluated to 30 <strong>and</strong> then this value is assigned to the variable c.<br />

Because the expression on the right side of an assignment statement is evaluated before<br />

an assignment is made, a statement such as<br />

n = n + 1<br />

is meaningful. It first evaluates the expression on the right (that is, it adds 1 to the original<br />

value of the variable n), <strong>and</strong> then assigns this sum to the variable n. The effect is to<br />

increase the value of the variable n by 1. In terms of memory locations, the statement<br />

retrieves the value of n from n’s memory location, uses it to compute n + 1, <strong>and</strong> then places<br />

the sum back into n’s memory location.<br />

■ PRINT METHOD<br />

Consider the following event procedure.<br />

Private Sub cmdDisplay_Click()<br />

picResults.Cls<br />

picResults.Print 3<br />

picResults.Print -3<br />

End Sub<br />

[Run <strong>and</strong> then click the comm<strong>and</strong> button.]

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

Saved successfully!

Ooh no, something went wrong!