7

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#?

4
  • Someones going for the Peer pressure badge ;)
    – SwDevMan81
    Jan 14, 2011 at 21:54
  • No, just switching from VB to C#. I took the time to learn the advanced stuff because its interesting, but I don't use C# enough to keep the standard syntax in my head. Jan 14, 2011 at 22:05
  • Take a look at the book "C# in a Nutshell" (O'Reilly) as a fast introduction -- it doesn't teach you much of the framework or how to program, but covers C# in detail.
    – Richard
    Jan 15, 2011 at 10:32
  • 1
    Even better than a book, try an automated conversion tool. If you know .NET, you're forgiven if you forget minor syntactical differences between C# and VB.NET. Jan 15, 2011 at 11:56

4 Answers 4

16

Sure:

do
{
    ...
} while (condition);

See do (C# Reference).

0
13
do
{
  // code
} while (condition)
0
-4

Alternatively

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

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

5
  • 2
    Bag your face, I am sure! do/while is a little sucky, but this sucks harder! Incidentally Pascal got it better with while/do and repeat/until which are differentiated even further by naming. Jan 14, 2011 at 22:14
  • 2
    Do/While makes it very plain that the loop should execute at least once. I really don't understand your reasoning for making things more complicated than they need to be.
    – Ed S.
    Jan 14, 2011 at 22:16
  • This is funny. I seem to have touched a real nerve here. You gonna mark me down for preferring switch over if-then-else? You should find something more important to nitpick about. You probably whine about 4 vs 3 vs 2 space indents, too. Jan 15, 2011 at 18:59
  • @David. I think VB has the best syntax for this construct. It doesn't matter if the condition is at the top, the bottom, or in the middle, the frame is always Do/Loop. Jan 20, 2011 at 9:35
  • I see extraneous variables, especially mutable variables, as a sign of sloppy thinking. Jan 20, 2011 at 9:37
-6
  TopOfLoop:
            // ...
            if (condition)
            {
                goto TopOfLoop;
            }

No career is complete without at least one goto.

2
  • That's how I coded it originally. Not intentionally mind you, the goto was originally called only under an obscure case. After finding two or three more reasons to call it I realized I was just writing a loop the hard way. Jan 20, 2011 at 9:33
  • If you can avoid using a goto, it is better to do so as it can make your code unreadable and hard to maintainable. Jan 9, 2021 at 21:30

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.