vb.net for loop

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 ]

Note: Bold denotes keywords. The rest is variables.

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 a For Loop with the help of a flow chart.

flow daigram vb.net

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

Following are the different examples

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:

output1 vb.net

 

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:

output2

 

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:

output3 vb.net

 

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:

output4 vb.net

 

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, examples and syntax breakdown. You can also go through our other related articles to learn more-

  1. While Loop in C
  2. For Loop in Python
  3. C# For Loop
  4. Loops in C

The post VB.Net for Loop appeared first on EDUCBA.