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


           
          


Continue in while

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