Illustrates the use of constants

image_pdfimage_print

   

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

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example2_7.cs illustrates the use of constants
*/

public class Example2_7
{

  public static void Main()
  {

    const int Length = 3;

    // mathematical constant pi
    const double Pi = 3.14159;

    // speed of light in meters per second
    const double SpeedOfLight = 2.99792e8;

    System.Console.WriteLine(Length);
    System.Console.WriteLine(Pi);
    System.Console.WriteLine(SpeedOfLight);

  }

}

           
          


Illustrates the use of a const field

image_pdfimage_print

   

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

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example6_2.cs illustrates the use of a const field
*/


// declare the Car class
class Car
{

  // declare a const field
  public const int wheels = 4;

}


public class Example6_2
{

  public static void Main()
  {

    System.Console.WriteLine("Car.wheels = " + Car.wheels);
    // Car.wheels = 5;  // causes compilation error

  }

}

           
          


Using break with nested loops

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Using break with nested loops.

using System;

public class BreakNested {
public static void Main() {

for(int i=0; i<3; i++) { Console.WriteLine("Outer loop count: " + i); Console.Write(" Inner loop count: "); int t = 0; while(t < 100) { if(t == 10) break; // terminate loop if t is 10 Console.Write(t + " "); t++; } Console.WriteLine(); } Console.WriteLine("Loops complete."); } } [/csharp]

Find the smallest factor of a value

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Find the smallest factor of a value.

using System;

public class FindSmallestFactor {
public static void Main() {
int factor = 1;
int num = 1000;

for(int i=2; i < num/2; i++) { if((num%i) == 0) { factor = i; break; // stop loop when factor is found } } Console.WriteLine("Smallest factor is " + factor); } } [/csharp]

the break statement

image_pdfimage_print

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

Publisher: Sybex;
ISBN: 0782129110
*/

/*
Example4_13.cs illustrates the use of
the break statement
*/

public class Example4_13
{

public static void Main()
{

int total = 0;

for (int counter = 1; counter <= 10; counter++) { System.Console.WriteLine("counter = " + counter); total += counter; if (counter == 5) { System.Console.WriteLine("break from loop"); break; } } System.Console.WriteLine("total = " + total); } } [/csharp]