C# Loops Demonstration (.Net)

This demonstration, adapted from the RemObjects Oxygene .Net Loops example, compiles in RemObjects C# (within Visual Studio) and in SharpDevelop. A copy of the output follows the code.

using System;
using System.Linq;

namespace loops_cs
{
  public class Country
  {
    public String Name{get;set;}    
    public  String Capital{get;set;}    

    public Country (String setName, String setCapital)
    {
      Name = setName;
      Capital = setCapital;
    }
  }	
	
  public class ConsoleApp
  {
    private static Country[] Countries = new Country[6];
    public static void Main(string[] args)    	
    {      
      fillData();
      loopsTesting();
      Console.ReadLine();
    }

    public static void fillData()
    {
      Countries[0] = new Country("UK", "London");
      Countries[1] = new Country("USA", "Washington");
      Countries[2] = new Country("Germany", "Berlin");
      Countries[3] = new Country("Ukraine", "Kyiv");
      Countries[4] = new Country("Russia", "Moscow");
      Countries[5] = new Country("France", "Paris");
    }

    public static void loopsTesting()
    {
      // "for" loop, taking every 5th item
      for (Int32 i = 0; i <= 50; i += 5) 
      {
        Console.Write(i); Console.Write(" ");
      }
      Console.WriteLine(); Console.WriteLine();

      // "for" loop, going from high to low value
      for (Int32 i= 10; i > 0; i--)
      {
        Console.Write(i); Console.Write(" ");
      }
      Console.WriteLine(); Console.WriteLine();      

      // foreach loop 
      Console.WriteLine("Countries: ");
      foreach (Country c in Countries)
      {
        Console.WriteLine(c.Name);
      }
      Console.WriteLine();
      Console.WriteLine("Cities: ");
      int ind = 0;

      // Loop endlessly until broken out of.
      while (true)
      {
        Console.WriteLine(Countries.ElementAt(ind).Capital);
        ind +=1;
        if (ind == Countries.Count()) 
          break;
      }
      Console.WriteLine();

      // the type of "c" is not inferred automatically
      foreach (Country c in Countries)
      {
        Console.WriteLine(c.Capital + " is the capital of " + c.Name);
      }
      Console.WriteLine();

      ind = 0;
      Console.WriteLine("Cities: ");

      // equivalent of "repeat ... until" loop
      do 
      { 
        Console.Write(Countries.ElementAt(ind).Capital + " ");
        ind += 1;
      }
      while (ind < Countries.Count());
      Console.WriteLine();

      ind = 0;
      Console.WriteLine("Countries: ");

      // typical "while" loop
      while (ind < Countries.Count())
      {
        Console.Write(Countries.ElementAt(ind).Name + " ");
        ind +=1;
      }
      Console.WriteLine();
    }
  }
}

Output:
0 5 10 15 20 25 30 35 40 45 50

10 9 8 7 6 5 4 3 2 1

Countries:
UK
USA
Germany
Ukraine
Russia
France

Cities:
London
Washington
Berlin
Kyiv
Moscow
Paris

London is the capital of UK
Washington is the capital of USA
Berlin is the capital of Germany
Kyiv is the capital of Ukraine
Moscow is the capital of Russia
Paris is the capital of France

Cities:
London Washington Berlin Kyiv Moscow Paris
Countries:
UK USA Germany Ukraine Russia France

Programming - a skill for life!

C# Demonstrations of I/O, arrays, methods, classes, loops, enums, pointers, file handling, Firebird databases, LINQ and drawing on a form