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.

EXAMPLE 2<br />

The following program illustrates the use of the assignment statement <strong>and</strong> Print method with text.<br />

Private Sub cmdShow_Click()<br />

picOutput.Cls<br />

phrase = “win or lose that counts.”<br />

picOutput.Print “It’s not whether you ”; phrase<br />

picOutput.Print “It’s whether I ”; phrase<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 />

It’s not whether you win or lose that counts.<br />

It’s whether I win or lose that counts.<br />

EXAMPLE 3<br />

The following program has strings <strong>and</strong> numbers occurring together in a picBalance.Print statement.<br />

Private Sub cmdCompute_Click()<br />

picBalance.Cls<br />

interestRate = 0.0655<br />

principal = 100<br />

phrase = “The balance after a year is”<br />

picBalance.Print phrase; (1 + interestRate) * principal<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 />

The balance after a year is 106.55<br />

■ CONCATENATION<br />

Two strings can be combined to form a new string consisting of the strings joined together. The<br />

joining operation is called concatenation <strong>and</strong> is represented by an ampers<strong>and</strong> (&). For instance,<br />

“good” & “bye” is “goodbye”. A combination of strings <strong>and</strong> ampers<strong>and</strong>s that can be<br />

evaluated to form a string is called a string expression. The assignment statement <strong>and</strong> Print<br />

method evaluate expressions before assigning them to variables or displaying them.<br />

EXAMPLE 4<br />

The following program illustrates concatenation.<br />

Private Sub cmdDisplay_Click()<br />

picQuote.Cls<br />

quote1 = “The ballgame isn’t over, ”<br />

quote2 = “until it’s over.”<br />

quote = quote1 & quote2<br />

picQuote.Print quote & “ Yogi Berra”<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 />

The ballgame isn’t over, until it’s over. Yogi Berra<br />

Strings 43

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

Saved successfully!

Ooh no, something went wrong!