For Next Loop - VB.NET - RIP Tutorial
Có thể bạn quan tâm
The For Next Loop is used to repeatedly execute a sequence of code or a block of code until a given condition is satisfied. It is useful in such a case when we know how many times a block of code has to be executed.
You use a For Next Loop when you want to repeat some statements a set number of times.
In the following example, the index variable starts with a value of 1 and is incremented with each iteration of the loop, ending after the value of the index reaches 10.
Public Sub Example1() For index As Integer = 1 To 10 Console.Write(index.ToString & ", ") Next Console.WriteLine("") End SubLet's consider another example in which the number variable starts at 5 and is reduced by 0.5 on each iteration of the loop, ending after the value of number reaches 0. The Step argument of -.5 reduces the value by 0.5 on each iteration of the loop.
Public Sub Example2() For number As Double = 5 To 0 Step -0.5 Console.Write(number.ToString & ", ") Next Console.WriteLine("") End SubNesting Loops
You can also nest For Next loops by putting one loop within another.
The following example shows nested For Next loops that have different step values.
Public Sub Example3() For indexA = 1 To 3 Dim sb As New System.Text.StringBuilder() For indexB = 20 To 1 Step -2 sb.Append(indexB.ToString) sb.Append(" ") Next indexB Console.WriteLine(sb.ToString) Next indexA End SubThe outer loop creates a string for every iteration of the loop. The inner loop decrements a loop counter variable for every iteration of the loop.
When nesting loops, each loop must have a unique counter variable.
Exit For and Continue For
The Exit For statement immediately exits the For Next loop and transfers control to the statement that follows the Next statement.
The Continue For statement transfers control immediately to the next iteration of the loop.
The following example shows the use of the Continue For and Exit For statements.
Public Sub Example4() For index As Integer = 1 To 100000 If index >= 5 AndAlso index <= 8 Then Continue For End If Console.Write(index.ToString & " ") ' If index is 10, exit the loop. If index = 10 Then Exit For End If Next Console.WriteLine("") End SubYou can put any number of Exit For statements in a For Next loop. When used within nested For Next loops, Exit For exits the innermost loop and transfers control to the next higher level of nesting.
Exit For is often used after you evaluate some condition such as an If...Then...Else structure.
Got any VB.NET Question?
Ask any VB.NET Questions and Get Instant Answers from ChatGPT AI: ChatGPT answer me!Từ khóa » Visual Basic For Next
-
For...Next Statement - Visual Basic - Microsoft Docs
-
For Each...Next Statement - Visual Basic - Microsoft Docs
-
VB.Net - For...Next Loop - Tutorialspoint
-
VB.NET For Next Loop - Javatpoint
-
Visual Basic For Next Statemen | The Coding Guys
-
VB.NET For Loop Examples (For Each) - Dot Net Perls
-
Visual Basic Tutorial - 26 - For Next Loop - YouTube
-
For Loop - For...Next Statement - VB.NET
-
Visual Basic (VB) For Loop - Tutlane
-
Looping In Visual Basic 2010
-
Visual Basic/Loops - Wikibooks, Open Books For An Open World
-
For...Next Statement
-
Visual Basic For Loops - Techotopia
-
For-Next Loop Visual Basic - Vba - Stack Overflow