
ADDITIONAL OPERATIONS WITH USER DEFINED VARIABLE TYPES
Consider the following code,
type Weekday = ( Monday, Tuesday, Wednesday, Thursday, Friday ); var Workday : Weekday;The first symbol of the set has the value of 0, and each symbol which follows is one greater. Pascal provides three additional operations which are performed on user defined variables. The three operations are,
ord( symbol ) returns the value of the symbol, thus ord(Tuesday)
will give a value of 1
pred( symbol ) obtains the previous symbol, thus
pred(Wednesday) will give Tuesday
succ( symbol ) obtains the next symbol, thus succ(Monday)
gives Tuesday
Enumerated values can be used to set the limits of a for
statement, or as a constant in a case statement, eg,
for Workday := Monday to Friday
.........
case Workday of
Monday : writeln('Mondays always get me down.');
Friday : writeln('Get ready for partytime!')
end;
Enumerated type values cannot be input from the keyboard or outputted
to the screen, so the following statements are illegal,
writeln( drink ); readln( chair );
type Day = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); var Today : Day; for Today := Sunday to Monday do begin writeln( Today ); Today := succ( Today ) end;Whats wrong with
type COLOR = ( Red, Blue, Green, Yellow ); var Green, Red : COLOR;Click here for answer
