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.

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

COMMENTS<br />

1. A condition involving numeric variables is different from an algebraic truth.<br />

The assertion (a + b) < 2 * a, considered in Example 2, is not a valid algebraic<br />

truth because it isn’t true for all values of a <strong>and</strong> b. When encountered in a <strong>Visual</strong><br />

<strong>Basic</strong> program, however, it will be considered true if it is correct for the current<br />

values of the variables.<br />

2. Conditions evaluate to either true or false. These two values often are called the<br />

possible truth values of the condition.<br />

3. A condition such as 2 < n < 5 should never be used, because <strong>Visual</strong> <strong>Basic</strong> will<br />

not evaluate it as intended. The correct condition is (2 < n) And (n < 5).<br />

4. A common error is to replace the condition Not (2 < 3) by condition (3 > 2).<br />

The correct replacement is (3 >= 2).<br />

4.2 IF BLOCKS<br />

An If block allows a program to decide on a course of action based on whether a certain condition<br />

is true or false. A block of the form<br />

If condition Then<br />

action1<br />

Else<br />

action2<br />

End If<br />

causes the program to take action1 if condition is true <strong>and</strong> action2 if condition is false. Each<br />

action consists of one or more <strong>Visual</strong> <strong>Basic</strong> statements. After an action is taken, execution<br />

continues with the line after the If block. Figure 4.1 contains the pseudocode <strong>and</strong> flowchart<br />

for an If block.<br />

If condition is true Then<br />

Processing step(s) 1<br />

Else<br />

Processing step(s) 2<br />

End If<br />

FIGURE 4-1 Pseudocode <strong>and</strong> Flowchart for an If Block<br />

EXAMPLE 1<br />

Write a program to find the larger of two numbers input by the user.

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

Saved successfully!

Ooh no, something went wrong!