Jump to content

[PASCAL] Ordinal types


CouldnoT
 Share

Recommended Posts

Ordinal types

 

Ordinal types are integer values that use literal indications instead of numbers. For example, if we need to define language variables (Romanian/English/French) we could use the value 1 for Romanian, 2 for English, and 3 for French. Other programmers wouldn't know the values for 1, 2 and 3 unless they find comments with these values. It will be more readable if we do it with ordinal types as in the below example:

 

program OrdinalTypes;
{$mode objfpc}{$H+}
uses
 {$IFDEF UNIX}{$IFDEF UseCThreads}
 cthreads,
 {$ENDIF}{$ENDIF}
 Classes
 { you can add units after this };
type
 TLanguageType = (ltRomanian, ltEnglish);
var
 Lang: TLanguageType;
 AName: string;
 Selection: Byte;
begin
 Write('Please select Language: 1 (Romanian), 2 (English)');
 Readln(Selection);
 if Selection = 1 then
 Lang:= ltRomanian
 else
 if selection = 2 then
 Lang:= ltEnglish
 else
 Writeln('Wrong entry');
 if Lang = ltRomanian then
 Write('Cum te numești: ')
 else
 if Lang = ltEnglish then
 Write('What is your name: ');
 Readln(AName);
 if Lang = ltRomanian then
 begin
  Writeln('Salut, ' , AName);
  Write('Vă rugăm să apăsați tasta Enter pentru a închide');
 end
 else
 if Lang = ltEnglish then
 begin
 Writeln('Hello ', AName);
 Write('Please press enter key to close');
 end;
 Readln;
end.

 

Integer, character and Boolean types are ordinal types, while real numbers and strings are not.

Edited by CouldnoT
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.