Csharp/C Sharp/Language Basics/While — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 14:39, 26 мая 2010

a do...while loop

<source lang="csharp"> /* 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);
 }

}


      </source>


Compute integer powers of 2

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// Compute integer powers of 2.

using System;

public class Power {

 public static void Main() { 
   int e; 
   int result; 

   for(int i=0; i < 10; i++) { 
     result = 1; 
     e = i; 

     while(e > 0) { 
       result *= 2; 
       e--; 
     } 

     Console.WriteLine("2 to the " + i +  
                        " power is " + result);        
   } 
 }   

}

      </source>


Compute the order of magnitude of an integer

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// Compute the order of magnitude of an integer

using System;

public class WhileDemo {

 public static void Main() { 
   int num; 
   int mag; 

   num = 435679; 
   mag = 0; 

   Console.WriteLine("Number: " + num); 

   while(num > 0) { 
     mag++; 
     num = num / 10; 
   }; 

   Console.WriteLine("Magnitude: " + mag); 
 }   

}


      </source>


Continue in while

<source lang="csharp"> /*

* 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 MyMainClass11
 {
   static void Main(string[] args)
   {
     WhileContinue();
     WhileBreak();
     WhileGoto();
   }
   static void WhileContinue()
   {
     int a = 0;
     
     while(a < 10)
     {
       a++;
       if (a == 5)
       {
         a++;
         continue;
       }
     }
   }
   static void WhileBreak()
   {
     int a = 0;
     while (a < 10)
     {
       a++;
       if (a == 5)
         break;
     }
     a++;
   }
   static void WhileGoto()
   {
     int a = 0;
     while (a < 10)
     {
       if (a == 5)
         goto cleanup;
     }
   cleanup :
     Console.WriteLine(a);
   }
 }

}

      </source>


Display the digits of an integer in reverse order

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// Display the digits of an integer in reverse order.

using System;

public class DoWhileDemo {

 public static void Main() { 
   int num; 
   int nextdigit; 

   num = 198; 

   Console.WriteLine("Number: " + num); 

   Console.Write("Number in reverse order: "); 

   do { 
     nextdigit = num % 10; 
     Console.Write(nextdigit); 
     num = num / 10; 
   } while(num > 0); 

   Console.WriteLine(); 
 }   

}

      </source>


Do While Tester

<source lang="csharp"> /* 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);
    }
}
          
      </source>


Simplest do while

<source lang="csharp"> /*

* 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);
   }
 }

}

      </source>


Simplest while

<source lang="csharp"> /*

* 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 Whiles
 {
   static void Main(string[] args)
   {
     int a = 0;
     
     while(a > 10)
     {
       a++;
       Console.WriteLine(a);
     }
   }
 }

}

      </source>


Using break to exit a do-while loop

<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852

  • /

// Using break to exit a do-while loop.

using System;

public class BreakDemo2 {

 public static void Main() {  
   int i; 

   i = -10;     
   do { 
     if(i > 0) break; 
     Console.Write(i + " "); 
     i++; 
   } while(i <= 10); 
 
   Console.WriteLine("Done");  
 }  

}

      </source>


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

<source lang="csharp"> /* 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;
   }
 }

}


      </source>


While loop to display 1 to 5

<source lang="csharp"> /* 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++;
   }
 }

}

      </source>


While Signal

<source lang="csharp"> /* Learning C# by Jesse Liberty Publisher: O"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\n");
                break;
            }
            if (signal == "0")
            {
                // normal traffic condition
                // log and continue on
                Console.WriteLine("All is well.\n");
                continue;
            }
            // Problem. Take action and then log the problem
            // and then continue on
            Console.WriteLine("{0} -- raise alarm!\n",
                signal);
        }
        return 0;
    }
}
          
      </source>


While Tester

<source lang="csharp"> /* Learning C# by Jesse Liberty Publisher: O"Reilly ISBN: 0596003765

  • /
using System;
public class WhileTester
{
    public static void Main()
    {
        int counterVariable = 0;
        // while the counter variable is less than 10
        // print out its value
        while (counterVariable < 10)
        {
            Console.WriteLine("counterVariable: {0}",counterVariable);
            counterVariable++;
        }
    }
}
          
      </source>


While true test

<source lang="csharp"> /* Learning C# by Jesse Liberty Publisher: O"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;
        }
    }
}
          
      </source>