For-In Loops

Introduction

Our tutorial on Pascal loops precedes those on arrays, sets and lists that are required knowledge for understanding for-in loops. The general loop is
for variable in collection do
  begin
    //lines to be iterated
  end;
The code is neater than the traditional for loops. This page demonstrates how to apply the for-in loops to arrays, strings, sets and lists where possible in Pascal, Smart Pascal and Oxygene for Java. In the case of Smart Pascal, we provide traditional alternatives for iterating through a string and a list.

For-In Loops in Pascal

For-in loops are available in Delphi 2005 onwards and in Lazarus (Free-Pascal). The program following the screenshot demonstrates loops for an array, a string, a set and a list.
Output

Output

program ForInPascal;

uses
  SysUtils, Classes;

const
  MONTH_NAMES : array[1 .. 12] of string = ('January', 'February', 'March', 'April', 'May', 'June', 'July',
                                            'August', 'September', 'October', 'November', 'December');
  MONTHS10 : set of byte = [4, 6, 9, 11];
  VOWELS : set of char = ['a', 'e', 'i', 'o', 'u'];
  STR : string = 'facetious';
var
  i, VowelCount : integer;
  month, colour : string;
  sl : TStringList;
  c : char;

begin
  writeln('All months:');
  //For-in loop for an array of strings
  for month in MONTH_NAMES do
    writeln(month);
  writeln;
  write('Months with 30 days:');
  //For-in loop for a set of byte
  for i in MONTHS10 do
    write(' ', MONTH_NAMES[i]);
  writeln;
  VowelCount := 0;
  //For-in loop for string
  for c in STR do
    if c in VOWELS then
     inc(VowelCount);
  writeln(#13#10'Number of vowels in ', STR, ': ', VowelCount);

  //For-in loop for a set of char
  write(#13#10'Vowels in first half of alphabet:');
  for c in VOWELS do
    if c < 'n' then
      write(' ', c);

  sl := TStringList.Create;
  sl.add('red');
  sl.add('orange');
  sl.add('yellow');
  write(#13#10#13#10'Colours:');
  //For-in loop for a string list
  for colour in sl do
    write(' ', colour);
  readln;
end.

For-In Loops in Smart Pascal

unit ForInDemo;

interface

uses 
  w3system, w3scroll, w3console, w3components, w3application, w3consoleapp, w3lists;

type
  TApplication = class(TW3CustomConsoleApplication)
  protected
    procedure PopulateConsole; override;
  end;
  TDay = (Sun, Mon, Tues, Wed, Thurs, Fri, Sat);

implementation

procedure TApplication.PopulateConsole;
const
  MONTH_NAMES  = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
                   'August', 'September', 'October', 'November', 'December'];
  MONTHS30 = [3, 5, 8, 10];
  STR = 'facetious';
var
  i, VowelCount, Earnings : integer;
  month, colour : string;
  sl : TStringList;
  WeekdaySet : set of TDay;
begin
  Console.writeln('All months: ');
  //FOR-IN loop for an array of strings
  for month in MONTH_NAMES do
    Console.writeln(month);
  Console.writeln('<br />Months with 30 days:');
  //FOR-IN loop for an array of integers
  for i in MONTHS30 do
    Console.writeln(' ' +  MONTH_NAMES[i]);
  Console.writeln('');
  WeekdaySet := [Mon, Tues, Wed, Thurs, Fri];
  Earnings := 0;
  //FOR-IN loop for a set based on an enumeration
  for i in WeekdaySet do
    begin
      Earnings += 111;
      Console.writeln('Earnings after day ' + intToStr(i) + ': £' + intToStr(Earnings));
    end;

  //Traditional FOR-FROM-TO loop for a string
  VowelCount := 0;
  for i := 1 to length(STR) do
    if STR[i] in ['a', 'e', 'i', 'o', 'u'] then
      inc(VowelCount);
  Console.writeln('<br />Number of vowels in ' + STR + ': ' + inttostr(VowelCount) + '<br />');
  sl := TStringList.Create;
  sl.add('red');
  sl.add('orange');
  sl.add('yellow');
  Console.writeln('Colours:');
  //for colour in sl do  //Fails, expecting array
  //  Console.writeln(colour);

  //Traditional FOR-FROM-TO loop for a string list
  for i := 0 to sl.Count - 1 do
    Console.writeln(sl.Items[i]);
end;

end.

For-In Loops in Oxygene for Java

The syntax for each variable in sequence is recommended, but for-in loops do not actually need the each keyword. The for-in loop works for a character array but not for a String (from which it can be derived by the String's toCharArray method. For use of the optional index and other examples of loops in Oxygene for Java see our language page.

namespace for_in_ofj;

interface

uses java.util;

implementation

const
  MONTH_NAMES : array[0 .. 11] of String = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
                                            'August', 'September', 'October', 'November', 'December'];
  MONTHS30 : array[0 .. 3] of Integer = [3, 5, 8, 10];	
  STR : String = 'facetious'; 
var
  i, VowelCount : Integer;
  month, colour : String;
  sl : List<String>;
  c : char;   

begin
  writeln('All months: ');
  for each month in MONTH_NAMES do
    writeln(month);
  writeln;	
  write('Months with 30 days:');
  for each i in MONTHS30 do
    write(' ' + MONTH_NAMES[i]);
  writeln;
	
  VowelCount := 0;	
  for each c in STR.toCharArray do
    if c in ['a', 'e', 'i', 'o', 'u'] then
      inc(VowelCount);
  writeln;	 
  writeln('Number of vowels in ' + STR + ': ' + VowelCount.toString);
  writeln;
	
  sl := new ArrayList<String>;
  sl.add('red');
  sl.add('orange');
  sl.add('yellow');
  writeln('Colours:');
  for each colour in sl do
    writeln(colour);
  for colour in sl do
    writeln(colour);
end. 

 

Programming - a skill for life!

Developments in Pascal, Oxygen for Java and Smart Mobile Studio