While Tester

image_pdfimage_print

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

image_pdfimage_print

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

image_pdfimage_print

   

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

image_pdfimage_print

   

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

image_pdfimage_print

   

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


           
          


Continue in 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 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); } } } [/csharp]

System maximums and minimums.

image_pdfimage_print
   
 


using System;

class MainClass {

    public static void Main() {
        // First, print out the minimum values
        Console.WriteLine("System Minimums
");
        Console.WriteLine("MinSByte {0}", System.SByte.MinValue);
        Console.WriteLine("MinByte {0}", System.Byte.MinValue);
        Console.WriteLine("MinInt16 {0}", System.Int16.MinValue);
        Console.WriteLine("MinUInt16 {0}", System.UInt16.MinValue);
        Console.WriteLine("MinInt32 {0}", System.Int32.MinValue);
        Console.WriteLine("MinUInt32 {0}", System.UInt32.MinValue);
        Console.WriteLine("MinInt64 {0}", System.Int64.MinValue);
        Console.WriteLine("MinUInt64 {0}", System.UInt64.MinValue);
        Console.WriteLine("MinChar {0}", System.Char.MinValue);
        Console.WriteLine("MinSingle {0}", System.Single.MinValue);
        Console.WriteLine("MinDouble {0}", System.Double.MinValue);
        // Console.WriteLine( "MinBoolean {0}", System.Boolean.MinValue);
        Console.WriteLine("MinDecimal {0}", System.Decimal.MinValue);

        Console.WriteLine("
System Maximums
");
        Console.WriteLine("MaxSByte {0}", System.SByte.MaxValue);
        Console.WriteLine("MaxByte {0}", System.Byte.MaxValue);
        Console.WriteLine("MaxInt16 {0}", System.Int16.MaxValue);
        Console.WriteLine("MaxUInt16 {0}", System.UInt16.MaxValue);
        Console.WriteLine("MaxInt32 {0}", System.Int32.MaxValue);
        Console.WriteLine("MaxUInt32 {0}", System.UInt32.MaxValue);
        Console.WriteLine("MaxInt64 {0}", System.Int64.MaxValue);
        Console.WriteLine("MaxUInt64 {0}", System.UInt64.MaxValue);
        Console.WriteLine("MaxChar {0}", System.Char.MaxValue);
        Console.WriteLine("MaxSingle {0}", System.Single.MaxValue);
        Console.WriteLine("MaxDouble {0}", System.Double.MaxValue);
        Console.WriteLine("MaxDecimal {0}", System.Decimal.MaxValue);
    }
}