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.

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

COMMENTS<br />

1. Numbers must not contain commas, dollar signs, or percent signs. Also, mixed<br />

numbers, such as 8 1/2, are not allowed.<br />

2. Some people think of the equal sign (=) in an assignment statement as an arrow<br />

pointing to the left. This stresses the fact that the value on the right is assigned<br />

to the variable on the left.<br />

3. Parentheses should be used when necessary to clarify the meaning of an expression.<br />

When there are no parentheses, the arithmetic operations are performed in<br />

the following order: (1) exponentiations; (2) multiplications <strong>and</strong> divisions;<br />

(3) additions <strong>and</strong> subtractions. In the event of ties, the leftmost operation is carried<br />

out first. Table 2-1 summarizes these rules.<br />

TABLE 2-1<br />

Level of Precedence for Arithmetic Operations<br />

( ) Inner to outer, left to right<br />

^ Left to right in expression<br />

*/ Left to right in expression<br />

+ – Left to right in expression<br />

4. Restricted keywords cannot be used as names of variables. For instance, the<br />

statements print = 99 <strong>and</strong> end = 99 are not valid. Some other common restricted<br />

keywords are Call, If, Let, Select, <strong>and</strong> Sub. If a keyword is used as a variable<br />

name, you will soon be warned that something is wrong. As soon as the cursor<br />

is moved from the line, an error message will appear, <strong>and</strong> the line will turn red.<br />

The use of some other keywords (such as Error, Height, Name, Rate, Time, Val,<br />

Width, <strong>and</strong> Year) as variable names does not trigger an immediate warning, but<br />

generates an error message when the program is run. Although there is a way to<br />

get <strong>Visual</strong> <strong>Basic</strong> to accept this last group of keywords as variable names, we<br />

will never use keywords as variable names. Most of the items in Appendix C,<br />

other than properties, are reserved words. You can tell immediately when you<br />

inadvertently use a reserved word as a variable in an assignment statement<br />

because <strong>Visual</strong> <strong>Basic</strong> automatically capitalizes the first letter of keywords. For<br />

instance, if you type “rate = 50” <strong>and</strong> press the Enter key, the line will change to<br />

“Rate = 50”.<br />

5. Grammatical errors, such as misspellings or incorrect punctuations, are called<br />

syntax errors. Certain types of syntax errors are spotted by the smart editor<br />

when they are entered, whereas others are not detected until the program is executed.<br />

When <strong>Visual</strong> <strong>Basic</strong> spots an error, it displays a dialog box. Some incorrect<br />

statements <strong>and</strong> their errors are given below.<br />

Statement Reason for Error<br />

picBox.Primt 3 Misspelling of keyword<br />

picBox.Print 2 + No number follows the plus sign<br />

9W = 5 9W is not a valid variable name<br />

6. Errors detected while a program is running are called run-time errors.<br />

Although some run-time errors are due to improper syntax, others result from<br />

the inability of the computer to carry out the intended task. For instance, if the<br />

value of numVar is 0, then the statement<br />

numVarInv = 1/numVar<br />

interrupts the program with the run-time error “Division by zero.” If the file<br />

DATA.TXT is not in the root folder of the C drive, then a statement that refers

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

Saved successfully!

Ooh no, something went wrong!