formlesstree4 18 Report post Posted December 19, 2009 In all programming languages, there are various ways to do a loop. Fortunately for you guys, I only care about how VB.net does these loops. These are the common loops: 1. The Do Until Loop Do Until <condition> 'Stuff here Loop Basically, the loop will continue until the condition is met. Example: Dim x As Integer = 0I Do Until x = 9 x += 1 Loop That loop will run until x = 9, and that will happen because I'm incrementing the Integer 'x' by 1 every time the loop goes through. 2. The Do While Loop Do While <condition> 'Stuff Loop This loop seems kind of redundant, because most things can be done with a Do Until Loop instead. The main difference is the Do Until Loop continues to run until the value is met, and the Do While Loop runs while the condition is met. Here are some rather sad examples of the Do loops: Dim conn As Boolean = False 'Do Until Loop Do Until conn = True CheckForInternet() Loop 'Do While Loop Do While conn = False CheckForInternet() Loop Obviously, the call to 'CheckForInternet()' doesn't matter here. I can say that that call will make conn = True, or stay false. The loops are actually the same result, it's just personal preference. You can also do this: Dim conn As Boolean = False Do CheckForInternet() Loop While conn = False Do CheckForInternet() Loop Until conn = True This actually will call the sub FIRST, then check if the condition applies, however this is hardly used and I don't see a good reason for that to even exist. 3. The For/For Each Loop Here's a For example Dim x As Integer = 0I For i = 0 To 4 x += 1 Next The regular For loop will continue to process the inside for a set amount of time. In this example, it will loop 5 times (0 to 4) while adding 1 to the variable 'x' each time. These kinds of loops allow for you to run certain functions for a set amount of time. The For loop will also accept variables into its counter: Dim x As Integer = 0I Dim i As Integer = 3I For i To i + 6 x += 1 Next As you can see, we can use 'i' as a predefined variable and tell the loop to run 6 + i times (which 6 + 3 = 9, so from 3 to 9, or 6 times), adding 1 to x each time. We can also even use the 'i' directly. Dim x As Integer = 0I Dim i As Integer = 3I For i To i + 6 x += i Next Whatever 'i' equals at this time will be added to the 'x' variable. This is a versatile loop. Now, this is a For Each Loop: 'Let's say I have an array with 5 items Dim y As String(4) 'From 0 to 4 For Each i As String In Y Messagebox.show(i) Next As you can see, using the 'i', we can tell VB.net to display the current string. This kind of loop can never have an IndexOutOfRange Exception because you are telling the loop to display each individual value in the Array of Strings. If you have any questions about these loops, or want me to go into more details, please let me know! That's all for now! Share this post Link to post Share on other sites
Polraudio 122 Report post Posted December 19, 2009 Wow i learned more about loops here than 3 weeks of class. Share this post Link to post Share on other sites
formlesstree4 18 Report post Posted December 22, 2009 Wow i learned more about loops here than 3 weeks of class. That's quite sad & rather scary considering you're paying for your classes in College and you learned more about Loops here on a forum... I feel special... Share this post Link to post Share on other sites