Loops

Introduction

We provide many examples of for loops, repeat loops and while loops in other sections of this tutorial. This code shows:
  • a step value as an increment in a for loop
  • a simple loop that will continue to repeat unless you break out of it
  • use of for each x in y (with an optional index value) to go through a collection

The program uses a generic list and a generic iterator to access each item in the list. In this case we use list<Country> but we could also use, say list<String> or list<Integer>. We cover generics again on the next page.

The Code

namespace Loops;

interface

uses java.util;

type
  ConsoleApp = class
  public
    class method Main(args : array of String);
    method loopsTesting;
    method fillData : List<Country>;
  private
    c : Country; 
    cList : List<Country>;
    it : Iterator<Country>;
  end;

type 
  Country = public class
  public
    property Name : String;
    property Capital : String;
    constructor (setName : String; setCapital : String);
  end;

implementation

class method ConsoleApp.Main(args : array of String);
begin
  System.out.println('Loops example');
  System.out.println;
  with myConsoleApp := new ConsoleApp  do
    myConsoleApp.loopsTesting;
end;

method ConsoleApp.loopsTesting;
begin
  {---------------------------------}
  {"for" loop, taking every 5th item}
  for i : Int32 := 0 to 50 step 5 do 
    begin
      System.out.print(i);
      System.out.print(' ');
    end;

  System.out.println; 
  System.out.println;

  {---------------------------------}
  {"for" loop, going from high to low value}
  for i : Int32 := 10 downto 1 do
    begin
      System.out.print(i); 
      System.out.print(' ');
    end;

  System.out.println;
  System.out.println;
  cList := fillData;
 
  {---------------------------------}
  {loop with defined "index" variable, which will count from 0 through the number of elements looped}
  {type of tempCountry and type of num inferred automatically}
  System.out.println('Countries: ');
  for each tempCountry in cList index num do 
    System.out.println((num + 1).toString + ') ' + tempCountry.Name);
  System.out.println;

  System.out.println('Cities: ');

  {---------------------------------}
  {simple "loop" construct that loops endlessly, until broken out of}
  it := cList.iterator;
  loop
    begin
      c := it.next;
      System.out.println(c.Capital);
      if not it.hasNext then
        break;
    end;
  System.out.println;

  {---------------------------------}
  {the type of tempCountry inferred automatically}
  for each tempCountry in cList do
    System.out.println(tempCountry.Capital + ' is the capital of ' + tempCountry.Name + '.');

  System.out.println;
  System.out.println('Cities: ');

  it := cList.iterator;
  {"repeat ... until" loop}
  repeat
    c := it.next;
    System.out.println(c.Capital);
  until not it.hasNext; 

  System.out.println;
  System.out.println('Countries: ');

  {---------------------------------}
  it := cList.iterator;
  while it.hasNext do
    begin
      c := Country(it.next);
      System.out.println(c.Name);
     end;

  System.in.read;
end;

method ConsoleApp.fillData: List<Country>;
begin
  result := new ArrayList<Country>;
  result.add(new Country('UK', 'London'));
  result.add(new Country('USA', 'Washington'));
  result.add(new Country('Germany', 'Berlin'));
  result.add(new Country('Ukraine', 'Kyiv'));
  result.add(new Country('Russia', 'Moscow'));
  result.add(new Country('France', 'Paris'));
end;

constructor Country (setName :String; setCapital: String);
begin
  Name := setName;
  Capital := setCapital;
end;

end.
Programming - a skill for life!

Language features such as loops, generics, class contracts, asynchronous methods, futures and Sugar in Oxygene for Java