Visual Basic Continue Statement

In visual basic, Continue statement is useful to transfer the control immediately to the next iteration of loops such as ForWhileDo-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.

Visual Basic Continue Statement Syntax

Following is the syntax of defining the Continue statement in visual basic programming language.

 

Continue Do | For | While }

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.

Visual Basic Continue Statement Flow Chart

Following is the pictorial representation of Continue statement process flow in visual basic programming language.

 

Visual Basic (VB) Continue Statement Flow Chart Diagram

 

Now, we will see how to use Continue statement in ForWhileDo-While statements with examples in visual basic programming language.

Visual Basic For Loop with Continue Statement

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 like as shown below.

 

Visual Basic Continue Statement with For Loop Example Result

 

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 we 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.

Visual Basic While Loop with Continue Statement

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 like as shown below.

 

Visual Basic Continue Statement with While Loop Example Result

 

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.

Visual Basic Do-While Loop with Continue Statement

Following is the example of using Continue keyword in Do-While loop to pass the control to the next iteration of 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 like 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.