Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between do while and while in VB.NET?

Tags:

What's the difference between Do While where the statement is the first line in the loop block and just the single While in VB.NET?

They don't seem to offer any difference in behavior.

like image 471
jaffa Avatar asked Apr 17 '13 10:04

jaffa


People also ask

What is the difference between while and do while?

The do-while loop is very similar to that of the while loop. But the only difference is that this loop checks for the conditions available after we check a statement. Thus, it is an example of a type of Exit Control Loop.

What is the main difference between while and do while loops?

In a nutshell, the structure of a while loop is very similar to that of a do-while loop, but the main difference lies in the fact that the while loop evaluates the condition first before executing the statements whereas the do-while loop executes the statements first before evaluating the condition of the loop.

Which is better while or do-while loop?

While loop because Python does not have do while loop. It depends on what you want to use it for. A while loop won't run if the condition is false, but a do-while loop will run at least once before it is terminated.


2 Answers

In Visual Basic these are identical:

    Dim foo As Boolean = True

    While Not foo
        Debug.WriteLine("!")
    End While

    Do While Not foo
        Debug.WriteLine("*")
    Loop

These are not; the do executes once:

    Dim foo As Boolean = True

    While Not foo
        Debug.WriteLine("!")
    End While

    Do
        Debug.WriteLine("*")
    Loop While Not foo
like image 139
dbasnett Avatar answered Oct 23 '22 08:10

dbasnett


In DO...WHILE, the code inside the loop is executed at least once

In WHILE Loop, the code inside the loop is executed 0 or more times.

like image 27
zeronillzero Avatar answered Oct 23 '22 08:10

zeronillzero