Visual Basic .NET - While Loop

In VB.NET, a while loop is used to execute a block of code repeatedly as long as the specified condition is true. The loop continues to execute until the condition becomes false. The syntax for the while loop is as follows:

While condition
    ' Block of code to be executed
End While

Here, the condition is a Boolean expression that is evaluated before each iteration of the loop. If the condition is true, the block of code inside the loop will be executed, and if the condition is false, the loop will be exited.

Let's take an example of using a while loop to print the numbers from 1 to 5:

Dim i As Integer = 1
While i <= 5
    Console.WriteLine(i)
    i += 1
End While

In this example, the loop will execute as long as the value of i is less than or equal to 5. The Console.WriteLine statement will print the value of i in each iteration of the loop, and the value of i will be incremented by 1 at the end of each iteration using the i += 1 statement.

Now, let's take another example of using a while loop to get input from the user until a valid input is entered:

Dim input As String = ""
While input <> "exit"
    Console.WriteLine("Enter a value or type 'exit' to quit:")
    input = Console.ReadLine()
    Console.WriteLine("You entered: " & input)
End While

In this example, the loop will continue to execute until the user enters the value "exit". Inside the loop, the user is prompted to enter a value, which is read using the Console.ReadLine statement. The entered value is then displayed back to the user using the Console.WriteLine statement.