Simplest do while

image_pdfimage_print

/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;

namespace Client.Chapter_4___Program_Control
{
public class DoWhile
{
static void Main(string[] args)
{
int a = 0;

do
{
a++;
Console.WriteLine(a);
} while (a < 10); } } } [/csharp]

While true test

image_pdfimage_print

   

/*
Learning C# 
by Jesse Liberty

Publisher: O&#039;Reilly 
ISBN: 0596003765
*/
 using System;
 public class WhileTrueTester
 {
     public static void Main()
     {
         int counterVariable = 0;  // initialization

         while (true)
         {
             Console.WriteLine(
                 "counter: {0} ", counterVariable++); // increment

             if (counterVariable > 10) // test
                 break;
         }
     }
 }

           
          


a do…while loop

image_pdfimage_print

   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example4_10.cs illustrates the use of
  a do...while loop
*/

public class Example4_10
{

  public static void Main()
  {

    int counter = 1;
    do
    {
      System.Console.WriteLine("counter = " + counter);
      counter--;
    }
    while (counter > 1);

  }

}


           
          


while loop to calculate and display the Fibonacci numbers less than 50

image_pdfimage_print

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example4_9.cs illustrates the use of
a while loop to calculate and display
the Fibonacci numbers less than 50
*/

public class Example4_9
{

public static void Main()
{

// initialize the first two numbers in the sequence
int oldNumber = 1;
int currentNumber = 1;

int nextNumber;

System.Console.Write(currentNumber + ” “);

while (currentNumber < 50) { System.Console.Write(currentNumber + " "); // calculate the next number by adding the // current number to the old number nextNumber = currentNumber + oldNumber; oldNumber = currentNumber; currentNumber = nextNumber; } } } [/csharp]

While loop to display 1 to 5

image_pdfimage_print

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
Example4_8.cs illustrates the use of
a while loop to display 1 to 5
*/

public class Example4_8
{

public static void Main()
{

int counter = 1;
while (counter <= 5) { System.Console.WriteLine("counter = " + counter); counter++; } } } [/csharp]

While Signal

image_pdfimage_print

   

/*
Learning C# 
by Jesse Liberty

Publisher: O&#039;Reilly 
ISBN: 0596003765
*/

 using System;
 public class WhileSignalTester
 {
     public static int Main()
     {
         string signal = "0";      // initialize to neutral
         while (signal != "X")      // X indicates stop
         {
             Console.Write("Enter a signal. X = stop. A = Abort: ");
             signal = Console.ReadLine();

             // do some work here, no matter what signal you
             // receive
             Console.WriteLine("Received: {0}", signal);

             if (signal == "A")
             {
                 // faulty - abort signal processing
                 // Log the problem and abort.
                 Console.WriteLine("Fault! Abort
");
                 break;
             }

             if (signal == "0")
             {
                 // normal traffic condition
                 // log and continue on
                 Console.WriteLine("All is well.
");
                 continue;
             }

             // Problem. Take action and then log the problem
             // and then continue on
             Console.WriteLine("{0} -- raise alarm!
",
                 signal);
         }
         return 0;
     }
 }
           
          


Do While Tester

image_pdfimage_print

/*
Learning C#
by Jesse Liberty

Publisher: O'Reilly
ISBN: 0596003765
*/

using System;
public class DoWhileTester
{
public static void Main()
{
int counterVariable = 11;

// display the message and then test that the value is
// less than 10
do
{
Console.WriteLine(“counterVariable: {0}”,counterVariable);
counterVariable++;
} while (counterVariable < 10); } } [/csharp]