tags:

views:

466

answers:

9

Hi I am new to programming in C# can some one give me an example of how to write do while loop in C#

EDIT: FYI I am a VB.NET programming trying to make the move to C#, so I do have experience with .NET / VB syntax. Thanks again!

+3  A: 
//remember, do loop will always execute at least once, a while loop may not execute at all
//because the condition is at the top
do
{
  //statements to be repeated
} while (condition);
David
Your while condition isn't a Boolean expression...
Jon Skeet
@Jon Skeet - sorry, I'm normally VB
David
do {} while (true);
Jhonny D. Cano -Leftware-
A: 

Hum, did you read any doc ?

C# do instruction

Learn to check the manual before asking ...

Thibault Falise
There's no reason to snipe at anyone.
Jason Punyon
right back at ya bud: http://stackoverflow.com/faq ;)
Chuck Conway
Sorry. I really didn't want to snipe him, but a simple google search would have given the answer ;-)
Thibault Falise
Come on! This is an easy cred question! It lets people up their cred by answering questions they know! +1 for the link to the documentation.
Atømix
@T.Falise: The founders' issues with "a simple google search" (instead of SO) can be found here - http://www.joelonsoftware.com/items/2008/09/15.html. The link you provide is 7th on the list for reasonable search terms "C# do while loop".
Jason Punyon
@Jason, good link! Being new here, I had not idea this site was so young.
Anthony Pegram
thanks falise for the advice.. yeah i was too quick to post here i should have read some msdn and googled before posting here.. i will try to do it from next time...
Srikrishna Sallam
+4  A: 
do
{
    if (someCondition)
    {
        someCriteria = false;
    }

} while (someCriteria);

But, yeah, it helps to RTM.

Anthony Pegram
+1  A: 

Here's a simple example that will print some numbers:

int i = 0;

do {
  Console.WriteLine(++i);
} while (i < 10);
John Feminella
+13  A: 

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.

Jon Skeet
And that's the key.. if you want that guaranteed first-time execution, use the do-while. Otherwise, while is indeed a better approach.
Anthony Pegram
Come on Skeet... ya gotta let the mortals answer the easy ones. :-)
Atømix
Why does all the easy questions that are being answered by Jon is being voted up to oblivion? :))@Jon: Please give us the chance to answer the easy ones. :)
Ian
@Ian because he's Jon Skeet
David
+1  A: 
using System;

class MainClass
{
    public static void Main()
    {
        int i = 0;
        do
        {
            Console.WriteLine("Number is {0}", i);
            i++;
        } while (i < 100);
    }
}

Another method would be

using System;

class MainClass
{
    public static void Main()
    {
        int i = 0;
        while(i <100)
        {
            Console.WriteLine("Number is {0}", i);
            i++;
        }
    }
}
Dremation
thanks for the clear explanation
Srikrishna Sallam
A: 

Apart from the Anthony Pegram's answer, you can use also the while loop, which checks the condition BEFORE getting into the loop

while (someCriteria)
{
    if (someCondition)
    {
        someCriteria = false;
        // or you can use break;
    }
    if (ignoreJustThisIteration)
    {
        continue;
    }
}
Jhonny D. Cano -Leftware-
A: 

Quite surprising that no one has mentioned yet the classical example for the do..while construct. Do..while is the way to go when you want to run some code, check or verify something (normally depending on what happened during the execution of that code), and if you don't like the result, start over again. This is exactly what you need when you want some user input that fits some constraints:

bool CheckInput(string input) { ... }
...
string input;
...
do {
  input=Console.ReadLine();
} while(!CheckInput(input));

That's quite a generic form: when the condition is simple enough, it's common to place it directly on the loop construct (inside the brackets after the "while" keyword), rather than having a method to compute it.

The key concepts in this usage are that you have to request the user input at least once (in the best case, the user will get it right at the first try); and that the condition doesn't really make much sense until the body has executed at least once. Each of these are good hints that do..while is the tool for the job, both of them together are almost a guarantee.

herenvardo
+1  A: 

Since you mentioned you were coming from VB.NET, I would strongly suggest checking out this link to show the comparisons. You can also use this wensite to convert VB to C# and vice versa - so you can play with your existing VB code and see what it looks like in C#, including loops and anything else under the son..

To answer the loop question, you simple want to do something like:

while(condition)
{
   DoSomething();
}

You can also do - while like this:

do
{
   Something();
}
while(condition);

Here's another code translator I've used with success, and another great C#->VB comparison website. Good Luck!

dferraro
thanks for the links to the comparison page.. this is just what i needed
Srikrishna Sallam