Wednesday, 7 April 2021

User Defined Ordinal Types | PPL | Sebesta | Elementary Data Types in Programming Language

                   In this post, we will see User Defined Ordinal Types | PPL | Sebesta | Elementary Data Types in Programming Language | user defined ordinal types, data types in ppl, data types ppl, elementary data types in programming language, ppl, sebesta, introduction  


4. User Defined Ordinal Types

       An ordinal type is one in which the range of possible values can be easily associated with the set of positive integers.

       In Java, for example, the primitive ordinal types are integer, char, and boolean.

       There are two user-defined ordinal types that have been supported by programming languages: enumeration and subrange.

 

i. Enumeration Types

              An enumeration type is one in which all of the possible values, which are named constants, are provided, or enumerated, in the definition.

              Values in enumeration type are called as enumeration constants.

 

Example: 

 

C or C++ Language

 

enum colors {red, blue, green, yellow, black};

colors myColor = blue, yourColor = red;

 

 

              C and Pascal were the first widely used languages to include an enumeration data type. C++ includes C’s enumeration types.

 

 

 

 

              C++ enumeration constants can appear in only one enumeration type in the same referencing environment.

 

              In Ada, enumeration literals are allowed to appear in more than one declaration in the same referencing environment. These are called overloaded literals.

 

              Enumeration types can provide advantages in both readability and reliability.

 

Examples:

 

C or C++ Language

enum colors {red, blue, green, yellow, black};

colors myColor = blue, yourColor = red;

 

Java

              All enumeration types in Java are implicitly subclasses of the predefined class Enum.

 

e.g.

 

class MyEnum

{

enum colors {red, blue, green, yellow, black}

 

public static void main(String args[])

{

colors myColor = colors.blue, yourColor = colors.red;

 

System.out.println("My Color is "+myColor.ordinal());

System.out.println("Your Color is "+yourColor.ordinal());

 

}

}

 

C# Language

 

enum days {Mon, Tue, Wed, Thu, Fri, Sat, Sun};

 

These values are implicitely assigned the integer values 0, 1, 2, 3....

 

ML

datatype weekdays = Monday | Tuesday | Wednesday | Thursday | Friday

 

F#

type weekdays = | Monday | Tuesday | Wednesday | Thursday | Friday

 

              Languages Perl, JavaScript, PHP, Python,

Ruby, and Lua do not allow enumeration data type.

 

 

 

ii. Subrange Types

              A subrange type is a contiguous subsequence of an ordinal type.

              For example, 12..14 is a subrange of integer type.

              Subrange types were introduced by Pascal and are included in Ada.

              Subrange types enhance readability by making it clear to readers that variables of subtypes can store only certain ranges of values.

              Reliability is increased with subrange types, because assigning a value to a subrange variable that is outside the specified range is detected as an error

 

Example:

 

Ada Language

              subranges are included in the category of types called subtypes.

 

type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);

subtype Weekdays is Days range Mon..Fri;

 

subtype Index is Integer range 1..100;

 

              All of the operations defined for the parent type are also defined for the subtype, except assignment of values outside the specified range.


Watch following video:

Watch on YouTube: https://www.youtube.com/watch?v=cODxVht0Ho0

No comments:

Post a Comment