Jonathan Allen
Jonathan Allen

Reputation: 70327

C#: While loops with the condition at the end

In VB I can write a loop that always executes at least once. For example:

Do
   [code]
Loop While [condition]

Is there a way to do that in C#?

Upvotes: 8

Views: 4106

Answers (4)

Ed Power
Ed Power

Reputation: 8531

  TopOfLoop:
            // ...
            if (condition)
            {
                goto TopOfLoop;
            }

No career is complete without at least one goto.

Upvotes: -6

Nicholas Carey
Nicholas Carey

Reputation: 74345

Alternatively

bool finished = false ;
while ( !finished )
{
   // do something
   finished = // evaluate a new foo
}

I've never been a huge fan of do/while

Upvotes: -4

Nemo157
Nemo157

Reputation: 3589

do
{
  // code
} while (condition)

Upvotes: 14

Mark Byers
Mark Byers

Reputation: 838974

Sure:

do
{
    ...
} while (condition);

See do (C# Reference).

Upvotes: 17

Related Questions