Visual Basic (VB) Do While Loop

In c#, Do-While loop is useful to execute the block of statements until the defined condition returns true.

 

Generally, in Visual Basic the do-while loop is same as while loop but only the difference is while loop will execute the statements only when the defined condition returns true but the do-while loop will execute the statements at least once because first it will execute the block of statements and then it will checks the condition.

Visual Basic Do-While Loop Syntax

Generally, in Visual Basic Do and While keywords are useful to create a do...while loop. Following is the syntax of defining a do-while loop in Visual Basic programming language to execute the block of statements till the defined condition evaluates as false.

 

Do

// Statements to Execute

Loop While boolean_expression

If you observe the above syntax, Do-While loop starts with Do keyword followed by the block of statements and While with a parameter called boolean_expression.

 

Here, the statements of Do-While loop will be executed first, after that the boolean_expression will be evaluated. if boolean_expression returns true, then again the statements inside of Do-While loop will be executed.

 

In case, if boolean_expression is evaluated to false, then the Do-While loop will stop the execution of statements and the program execution will come out of the loop.

Visual Basic Do While Loop Flow Chart

Following is the pictorial representation of Do-While loop process flow in Visual Basic programming language.

 

Visual Basic Do While Loop Flow Chart Diagram

 

Now, we will see how to use Do-While loop in Visual Basic programming language with examples.

Visual Basic Do While Loop Example

Following is the example of using Do-While loop in Visual Basic programming language to execute the block of statements based on our requirements.

 

Module Module1

    Sub Main()

        Dim i As Integer = 1

        Do

            Console.WriteLine("i value: {0}", i)

            i += 1

        Loop While i <= 4

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, first we are executing the statements within the Do-While loop and increasing the variable i (i++) value to 1 by using increment operator.

 

After that, the condition (i <= 4) will be evaluated and again it will execute the block of statements in case the defined condition returns true otherwise it will terminate the loop.

 

When we execute the above Visual Basic program, we will get the result like as shown below.

 

Visual Basic Do-While Loop Example Result

 

If you observe the above result, Do-While loop has executed until it matches the defined condition (i <= 4) and the program execution came out of the loop whenever the defined condition returns false.

Visual Basic Nested Do-While Loop

In Visual Basic, we can use one Do-While loop within another Do-While loop to implement the application based on our requirements.

 

Following is the example of implementing nested Do-While loop in Visual Basic programming language.

 

Module Module1

    Sub Main()

        Dim i As Integer = 1

        Do

            Console.WriteLine("i value: {0}", i)

            i += 1

            Dim j As Integer = 1

            Do

                Console.WriteLine("j value: {0}", j)

                j += 1

            Loop While j < 2

        Loop While i < 4

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we used one Do-While loop within another Do-While loop to achieve the nested do-while loop functionality in our application based on our requirements.

 

When we execute the above Visual Basic program, we will get the result like as shown below.

 

Visual Basic Nested Do While Loop Example Result

 

If you observe the above result, both do-while loops got executed and returned the result based on our requirements.

Visual Basic Do-While Loop with Exit Statement

In Visual Basic, we can exit or terminate the execution of Do-While loop immediately by using Exit keyword.

 

Following is the example of using Exit keyword in Do-While loop to terminate the execution 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 Exit 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 terminating the loop using Exit statement.

 

When we execute the above Visual Basic program, we will get the result like as shown below.

 

Visual Basic Do While Loop with Break Statement Example Result

 

This is how we can use the Exit statement with Do-While loop to terminate the execution of loop based on our requirements.