Page 146 - Open Soource Technologies 304.indd
P. 146
Event Driven Programming
Comments
1. Be careful to avoid infinite loops that is, loops that are never exited. The following loop
is infinite, because the condition “balance < 1000” will always be true.
Click on the form’s Close button at the upper right corner of the title bar to
end the program.
Private Sub btnButton_Click(...) Handles btnlick
‘An infinite loop
Dim balance As Double = 100, intRate As Double
Do While balance < 1000
balance = (1 + intRate) * balance
Loop
txtBalance.Text = FormatCurrency(balance)
End Sub
Notice that this slip-up can be avoided by adding something like = 0.04 to the end of the
Dim statement.
2. Visual Basic allows the use of the words “While” and “Until” at either the top or bottom
of a Do loop. In this text, the usage of these words is restricted for the following reasons:
a. Because any While statement can be easily converted to an Until statement and vice
versa, the restriction produces no loss of capabilities and the programmer has one less
matter to think about.
b. Restricting the use simplifies reading the program. The word “While” proclaims
testing at the top, and the word “Until” proclaims testing at the bottom.
c. Certain other major structured languages only allow “While” at the top and “Until”
at the bottom of a loop. Therefore, following this convention will make life easier for
people already familiar with or planning to learn one of these languages.
d. Standard pseudocode uses the word “While” to denote testing a loop at the top and
the word “Until” to denote testing at the bottom.
6.4 While-Wend
The While...Wend statement repeats a block of code while a condition is true. The condition is
always evaluated before entering the loop. If it is not fulfilled, the loop is never executed.
It is possible to exit a While...Wend loop at any moment using the Exit While statement. Control
is then transferred to the first line following the Wend keyword. In the case of nested loops, the
Exit While statement only exits the loop in which it is used and the external loop continues
executing normally.
The following example uses a loop to list the graphics modes supported by the PDA.
Private Sub Button1_Click()
140 LOVELY PROFESSIONAL UNIVERSITY