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.

■ ARITHMETIC OPERATIONS<br />

The five arithmetic operations in <strong>Visual</strong> <strong>Basic</strong> are addition, subtraction, multiplication, division,<br />

<strong>and</strong> exponentiation. (Because exponentiation is not as familiar as the others, it is reviewed<br />

in detail in Comment 10.) Addition, subtraction, <strong>and</strong> division are denoted in <strong>Visual</strong><br />

<strong>Basic</strong> by the st<strong>and</strong>ard symbols +, –, <strong>and</strong> /, respectively. However, the notations for multiplication<br />

<strong>and</strong> exponentiation differ from the customary mathematical notations.<br />

Mathematical Notation <strong>Visual</strong> <strong>Basic</strong> Notation<br />

a � b or a � b a * b<br />

ar a ^ r<br />

(The asterisk [*] is the upper character of the 8 key. The caret [^] is the upper character of the<br />

6 key.) Note: In this book, the proportional font used for text differs from the monospaced font<br />

used for programs. In the program font, the asterisk appears as a five-pointed star (*).<br />

One way to show a number on the screen is to display it in a picture box. If n is a number,<br />

then the instruction<br />

picBox.Print n<br />

displays the number n in the picture box. If the picBox.Print instruction is followed by a combination<br />

of numbers <strong>and</strong> arithmetic operations, it carries out the operations <strong>and</strong> displays the<br />

result. Print is a reserved word <strong>and</strong> the Print operation is called a method. Another important<br />

method is Cls. The statement<br />

picBox.Cls<br />

erases all text <strong>and</strong> graphics from the picture box picBox.<br />

EXAMPLE 1<br />

The following program applies each of the five arithmetic operations to the numbers 3 <strong>and</strong> 2. Notice that<br />

3/2 is displayed in decimal form. <strong>Visual</strong> <strong>Basic</strong> never displays numbers as common fractions. Note 1: The<br />

star in the fifth <strong>and</strong> eighth lines is the computer font version of the asterisk. Note 2: The word “Run” in<br />

the phrasing [Run ...] indicates that F5 should be pressed to execute the program. Note 3: All programs<br />

appearing in examples <strong>and</strong> case studies are provided on the CD accompanying this book. See the discussion<br />

on the next to last page of the book for details.<br />

Below is the form design <strong>and</strong> a table showing the names of the objects on the form <strong>and</strong> the settings, if<br />

any, for properties of these objects. This form design is also used in the discussion <strong>and</strong> examples in the<br />

remainder of this section.<br />

Private Sub cmdCompute_Click()<br />

picResults.Cls<br />

picResults.Print 3 + 2<br />

picResults.Print 3 - 2<br />

picResults.Print 3 * 2<br />

picResults.Print 3 / 2<br />

picResults.Print 3 ^ 2<br />

picResults.Print 2 * (3 + 4)<br />

End Sub<br />

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

Object Property Setting<br />

frm3_3_1 Caption, 3-3-1<br />

picResults<br />

cmdCompute Caption Compute<br />

Numbers 35

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

Saved successfully!

Ooh no, something went wrong!