Do .. While loop in C#?

后端 未结 8 1196
后悔当初
后悔当初 2020-12-18 18:21

How do I write a Do .. While loop in C#?

(Edit: I am a VB.NET programmer trying to make the move to C#, so I do have experience with .NET / VB syntax. Thanks!)

8条回答
  •  南笙
    南笙 (楼主)
    2020-12-18 19:04

    The general form is:

    do
    {
       // Body
    } while (condition);
    

    Where condition is some expression of type bool.

    Personally I rarely write do/while loops - for, foreach and straight while loops are much more common in my experience. The latter is:

    while (condition)
    {
        // body
    }
    

    The difference between while and do...while is that in the first case the body will never be executed if the condition is false to start with - whereas in the latter case it's always executed once before the condition is ever evaluated.

提交回复
热议问题