Enumerated Types

Introduction to Enumerated Types


Enumerated Types Create New Literals!

Enumerated Types - Questions

Operations Available with Enumerated Types

Some Basic Operations

  • Example (prettified)
        -- Illustrates:
        --    declaring variables, assignment, relational operators, in, ranges
        with Ada.Text_io; use Ada.Text_io;
        procedure enum1 is
            type Day is (Sunday, Monday, Tuesday, Wednesday, 
                            Thursday, Friday, Saturday);
            today: Day;
            today := Thursday;
            if today = Friday then
                put("We made it!");
            end if;
            for d in Sunday .. Saturday loop
                if d in Monday .. Friday then
                    put("It's a work day.");
                    if <  Wednesday then 
                        put("Just started the week!.");
                    end if;
                    put("It's the weekend!");
                end if;
            end loop;
        end enum1;

    I/O of Enumerated Types

    Advantages of Enumeration_IO

    Package Enumeration_IO

    More Operations: Attributes of Enumerated Types

    Type Checking with Enumerated Types

    Enumerated Types are Strongly Typed

            with Ada.Text_io; use Ada.Text_io;
            procedure enum1a is
            type Color is (Red, Blue, Green);
            type Day is (Sunday, Monday, Tuesday, Wednesday, 
                            Thursday, Friday, Saturday);
            c: Color;
            d: Day;
            n: Natural := 1;
            c := Red;
            d := Friday;
            c := Friday;          -- Compile error
            c := d;               -- Compile error
            c := 1;               -- Compile error
            d := 1;               -- Compile error
            n := c;               -- Compile error
            c := n;               -- Compile error
            if d = Blue then      -- Compile error
                put("It's Friday!");
            end if;
            end enum1a;

    Ambiguous Values

    Enumerated Type Values and Variables, etc

    Array Indices and Subranges

    Enumerated Types as Array Indices

    Subtypes (ie Subranges) of Enumeration

    Predefined Enumeration Types

    Types Boolean and Character

    Enumerated Types in Other Languages

    Java 1.4 and Earlier (and Sometimes Later)

    C and C++

    Java 1.5 and Later

    Motivation for Enumerated Types


    A Final Example using Enumerated Types

    An Example Using Enumeration Types