In visual basic, Continue statement is useful to transfer the control immediately to the next iteration of loops such as For, While, Do-While from the specified position by skipping the remaining code.
In the previous section, we learned the Exit statement in vb. The main difference between the Exit statement and Continue
statement is, the Exit statement will completely terminate the loop or statement execution but the Continue
statement will transfer the control immediately to the next iteration of the loop.
Following is the syntax of defining the Continue
statement in visual basic programming language.
In our applications, we can use Continue
statement whenever we want to skip the execution of code from the particular position and send back the control to the next iteration of loop based on our requirements.
Following is the pictorial representation of Continue
statement process flow in visual basic programming language.
Now, we will see how to use Continue statement in For, While, Do-While statements with examples in visual basic programming language.
In visual basic, by using Continue
keyword we can skip the execution of further code and send back the control to next iteration of For loop statement based on our requirements.
Following is the example of using Continue
statement with For loop in visual basic programming language.
Module Module1
Sub Main()
For i As Integer = 1 To 4
If i = 3 Then Continue For
Console.WriteLine("i value: {0}", i)
Next
Console.WriteLine("Press Enter Key to Exit..")
Console.ReadLine()
End Sub
End Module
If you observe the above code, we used Continue
statement to pass the control back to the next iteration of For loop whenever the variable i value equals to 3.
When we execute the above visual basic program, we will get the result as shown below.
If you observe the above result, whenever the variable i value equals to 3, it skips the further execution of statements and passes the control back to the next iteration of For loop.
This is how we can use Continue
statement in For loop to skip the further execution of statements and send back the control to further iteration of For loop based on our requirements.
In visual basic, we can stop the execution of further statements from the specified position and send back the control to the further iteration of While loop immediately.
Following is the example of using Continue
keyword in While loop to pass the control to the next iteration of the loop in a visual basic programming language.
Module Module1
Sub Main()
Dim i As Integer = 0
While i < 4
i += 1
If i = 2 Then Continue While
Console.WriteLine("i value: {0}", i)
End While
Console.WriteLine("Press Enter Key to Exit..")
Console.ReadLine()
End Sub
End Module
If you observe the above example, whenever the variable (i) value become 2 we are skipping the further execution of statements and passing the control back to the further iteration of While loop using Continue
statement.
When we execute the above visual basic program, we will get the result as shown below.
This is how we can use the Continue
statement with While loop to pass the control back to the further iteration of loop based on our requirements.
Following is the example of using Continue
keyword in the Do-While loop to pass the control to the next iteration of the loop in visual basic programming language.
Module Module1
Sub Main()
Dim i As Integer = 1
Do
Console.WriteLine("i value: {0}", i)
i += 1
If i = 2 Then Continue Do
Loop While i < 4
Console.WriteLine("Press Enter Key to Exit..")
Console.ReadLine()
End Sub
End Module
If you observe the above example, whenever the variable (i) value become 2 we are skipping the further execution of statements and passing the control back to the further iteration of loop using Continue
statement.
When we execute the above visual basic program, we will get the result as shown below.
i value: 1
i value: 2
i value: 3
Press Enter Key to Exit..
This is how we can use the Continue
statement with Do-While loop to pass the control back to the further iteration of loop based on our requirements.