While Signal


   

/*
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
");
                 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

/*
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]

While Tester

/*
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++; } } } [/csharp]

Using break to exit a do-while loop

/*
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"); } } [/csharp]

Display the digits of an integer in reverse order


   

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

           
          


Compute integer powers of 2


   

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

           
          


Compute the order of magnitude of an integer


   

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