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.

Similarly, the statements<br />

Do While flagVar = True <strong>and</strong> Do While flagVar = False<br />

can be replaced by<br />

Do While flagVar <strong>and</strong> Do While Not flagVar<br />

5.3 FOR...NEXT LOOPS<br />

When we know exactly how many times a loop should be executed, a special type of loop,<br />

called a For...Next loop, can be used. For...Next loops are easy to read <strong>and</strong> write, <strong>and</strong> have<br />

features that make them ideal for certain common tasks. The following code uses a For...Next<br />

loop to display a table.<br />

Private Sub cmdDisplayTable_Click()<br />

Dim i As Integer<br />

‘Display a table of the first 5 numbers <strong>and</strong> their squares<br />

picTable.Cls<br />

For i = 1 To 5<br />

picTable.Print i; i ^ 2<br />

Next i<br />

End Sub<br />

[Run <strong>and</strong> click on cmdDisplayTable. The following is displayed in the picture box.]<br />

1 1<br />

2 4<br />

3 9<br />

4 16<br />

5 25<br />

The equivalent program written with a Do loop is as follows.<br />

Private Sub cmdDisplayTable_Click()<br />

Dim i As Integer<br />

‘Display a table of the first 5 numbers <strong>and</strong> their squares<br />

picTable.Cls<br />

i = 1<br />

Do While i

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

Saved successfully!

Ooh no, something went wrong!