Updated March 17, 2023
Introduction on VB.Net for Loop
We all know about For loops. They are used to execute a set of statements again and again for a specific number of times. This is exactly what For loops in VB.Net achieve. The For Loop in VB.Net is also referred to as For Next Loop. This is because the syntax of the loop completes with a Next statement.
Syntax:
For counter [ As datatype ] = start To end [ Step step ]
[ statements ]
[Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ counter ]
Syntax Breakdown
Notice the number of jargon in the syntax! Worry not, we will understand each of them and their purpose in the syntax.
- For: The keyword that marks the beginning of the loop definition.
- counter: The variable that would be used as the control variable of the loop. The value of this variable would determine when to exit the loop. It must be a numeric value only.
- As datatype: The data type of the counter variable. This is important for VB .Net to compare the value of the counter variable with the end expression in each iteration. If the data type is not specified, it is inferred from the start, end and, step variables.
- start To end: The numeric expressions that denote the initial and final values of the counter variable. When the loop execution begins, the initial value of the counter variable is set by the start expression. Before each iteration, the current value of the counter variable is compared with the end expression. If the value of the counter exceeds the end, the loop execution is terminated.
- Step: Determines the value by which the counter variable is incremented or decremented after each iteration. If not specified, the default value is 1.
- statements: The set of statements to be executed during every iteration of the for a loop. Of course, they are required. What else would you be writing a for loop for?
- Continue For: Skips the remaining set of statements in the current iteration and continues with the next iteration of the loop.
- Exit For: Breaks out of them for a loop.
- Next: The keyword that marks the end of the loop definition.
Flow Diagram
Let us understand the working of For Loop in VB.Net with the help of a flow chart.
How For Loop Works In VB.Net?
Above is a simple flow diagram of basic For Loop. The very first step is to initialize the counter variable with the start value. It is then compared with the end expression. If the end expression results in true, the control enters the loop body and statements are executed. If the end expression results in false, the control exits the loop. After each iteration, the value of the counter variable is updated by the compiler automatically.
There are other optional elements in a VB .Net For Loop as well. The step variable is used to override the default increment/decrement value of 1. You can specify any numeric value in the step variable and the counter variable will be updated by that numeric value.
The Continue For statement is used when you don’t want an iteration of the loop to follow the same flow as other iterations. An example would be when you want to print the prime numbers from 1 to 100, the very first condition you would check is if the number is even and not 2 then you would not print the number and continue the next iteration of the loop.
The Exit For statement is useful when you want to exit the loop in the middle of an iteration and not proceed with any further iterations. An example of this would be when you are searching for an element in an array of 100 elements if the element is found at 35th position you would not want to execute the remaining 65 iterations of the loop.
Examples of VB.Net For Loop
Following are the different examples of VB.Net For Loop:
Example #1
Below is a simple example to print the square of all numbers from 1 to 10 in descending order.
Code:
Imports System
Public Module Module1
Public Sub Main()
For num As Integer = 10 To 1 Step -1
Console.WriteLine("Square of " & num & " is " & num * num)
Next
End Sub
End Module
Output:
Example #2
The below example illustrates the use of Continue For statement. The code below would not print the square of even numbers. Of course, a more efficient way to achieve this result would be to change the step variable to 2.
Code:
Imports System
Public Module Module1
Public Sub Main()
For num As Integer = 10 To 1 Step -1
If num Mod 2 = 0 Then
Continue For
End IF
Console.WriteLine("Square of " & num & " is " & num * num)
Next
End Sub
End Module
Output:
Example #3
The below example illustrates the use of Exit For statement. The code below exits the loop when the number is a multiple of 5. It does not do so for the number 10, because the Continue For statement prevents the execution of Exit For statement in the case of 10.
Code:
Imports System
Public Module Module1
Public Sub Main()
For num As Integer = 10 To 1 Step -1
If num Mod 2 = 0 Then
Continue For
End IF
If num Mod 5 = 0 Then
Exit For
End IF
Console.WriteLine("Square of " & num & " is " & num * num)
Next
End Sub
End Module
Output:
Example #4
Bonus Example: Nested For Loops. The below example illustrates the use of nested For Loop to print a pattern.
Code:
Imports System
Public Module Module1
Public Sub Main()
For num1 As Integer = 10 To 1 Step -1
For num2 As Integer = num1 To 1 Step -1
Console.Write("*")
Next
Console.WriteLine("")
Next
End Sub
End Module
Output:
Conclusion
Loops are a very important utility in any programming language. They help us execute repetitive statements with minimal code. The article above has provided a thorough in-depth understanding of For Loops in VB.Net. Sans syntax, the working of For Loops is similar in any other programming language. The next task for you is to try more examples with For Loops and nested For Loops. Tweak the conditions, create your problems and solve them using For Loop in the most efficient way possible. This will help you develop your own understanding of loops.
Recommended Articles
This is a guide to VB.Net for Loop. Here we discuss how a loop works in vb.net, and how it helps us execute repetitive statements with minimal code along with flow diagram. You can also go through our other related articles to learn more-