![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
To terminate the loop early, use Exit Do. Heres a loop that will execute until Y is greater than zero or X is less than zero: Do ... If X < 0 Then Exit Do ... Loop Until Y > 0 You may be thinking that we could have obtained the same result by writing the loop like this: Do ... Loop Unt/il Y > 0 Or X < 0 You are correct, but a subtle difference exists between these two loops. In the first example, the statements between the If X < 0 statement and the Loop statement are not executed during the last loop iteration, when X becomes less than zero. In the second example, all of the statements in the loop are executed during the last execution. While...Wend The While...Wend loop executes a block of statements as long as a specified Condition is True, but it is not nearly as flexible as Do...Loop. In fact, anything that While...Wend can do, Do...Loop can also accomplish. You may find While...Wend in older Basic programs, simply because earlier versions of Basic did not support Do...Loop. Heres the way to write a While...Wend loop: While Condition ... statement block ... Wend You can write all of your Basic programs without ever needing a While...Wend loop. Take a look at the following variant of Do...Loop, which does exactly the same thing as a While...Wend loop: Do While Condition ... statement block ... Loop Because of its added flexibility, I recommend that you use Do...Loop rather than the While...Wend structure found in older programs. Nested And Infinite Loops A nested loop is a loop contained within another loop. There is no limit to nesting of loops in Visual Basic, although nesting beyond four or five levels is rarely necessary or even advisable. If your program seems to need such deep nesting, you should probably reexamine its structure to see if you can accomplish the same task more simply. The only restriction on nesting loops is that each inner loop must be enclosed entirely within the outer loop. The following example is illegal, because the For...Next loop is not contained entirely within the Do...Loop loop: Do While X > 0 For I = 1 to 10 ... Loop Next I The next example, however, is okay: Do While X > 0 For I = 1 to 10 ... Next I Loop An infinite loop is one that executes forever (or until you halt the program). Clearly, if a loops terminating condition is never met, it will loop indefinitely. This situation can arise from faulty program logic or from unexpected user input. Keep a sharp eye out for this sort of problem. If your program seems to hang during execution, you might want to examine your loops. With End With The With End With statement provides a convenient shorthand when you want to access more than one property or method of an object. The syntax is: With object .Property1 = ... .Property2 = ... .Method1 End With The preceding code has the same effect as the following: object.Property1 = ... object.Property2 = ... object.Method1 When used simply as a shorthand, the With End With statement is at best a minor convenience. It is more useful when used in procedures that take an object as a parameter. Here, for example, is a procedure that will change several font-related properties of any object that is passed to it: Public Sub ChangeFont(ob As Object) With ob .Font.Bold = True .Font.Italic = True .ForeColor = RGB(255, 0, 0) End With End Sub But Wait, Theres MoreIf youve managed to read this entire chapterand I suggest that you do read it closelyyou should have a good introduction to the foundations of the Basic programming language. Yes, of course, Basic continues far beyond this point. Does that mean this chapter is going to stretch on for another 40 pages? Not to worry. I suspect that your patience is wearing thin and you are itching to try some of the things you have learned. The remaining details of Basic will be covered as individual topics arise. In the next chapter, well put together a Visual Basic application that demonstrates how Basic code, objects, and properties combine to create an application.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |