CSharp examples for Language Basics:do while
The format of the do statement is shown here:
Do
{
Statement(s)
} while ( condition );
The following code shows how to get random numbers (from 1 to 10) until a 5 is reached.
class MainClass//from ww w .j a v a2 s.co m { public static void Main() { int ttl = 0; int nbr = 0; int ctr = 0; System.Random rnd = new System.Random(); do { //Get random number nbr = (int) rnd.Next(1,11); ctr++; ttl += nbr; System.Console.WriteLine("Number {0} is {1}", ctr, nbr); } while ( nbr != 5 ); System.Console.WriteLine("\n{0} numbers were read", ctr); System.Console.WriteLine("The total of the numbers is {0}", ttl); System.Console.WriteLine("The average of the numbers is {0}", ttl/ctr ); } }