Types and Variables - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

Types and Variables

Description:

Types and Variables. A type is simply a template for what a variable in memory will look like. ... Reminder: number : constant integer := 10; -- creates a ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 9
Provided by: maci176
Category:

less

Transcript and Presenter's Notes

Title: Types and Variables


1
Types and Variables
  • A type is simply a template for what a variable
    in memory will look like. Declaring a type does
    not allocate memory for storage.
  • A variable is a named location in memory that has
    been declared to contain a particular type of
    data.

2
Ada Predefined Types
  • Boolean (true or false)
  • Integer (whole numbers e.g. 1,2,-3)
  • Float (real numbers e.g. 1.12, -0.37)
  • Character (any ASCII character e.g. a, b, tab)
  • String() (a string of characters of length )
  • Natural (non-negative integers)

3
Variable Declarations
  • Examples
  • flag boolean -- can be either true or false
  • number integer -- can be any whole number
    -- including negative numbers
  • realnum float -- can be any real number
  • -- including negative numbers
  • somechar character -- can be any ASCII
    character
  • somestring string(1..20) -- is a string of 20
    characters
  • Reminder
  • number constant integer 10 -- creates a
  • -- memory location named number with the value
    10 that -- cannot be changed

4
User Defined Subtypes
  • Form
  • Subtype typename is basetype in range
    start..stop
  • Where typename is the name of the subtype (e.g.
    age_type)
  • basetype is the name of the original type (e.g.
    integer)
  • start is a starting position for the range
    (e.g. 0)
  • stop is a stopping position for the range (e.g.
    130)
  • Example
  • Subtype age_type is integer range 0..130

5
Enumerated Types
  • Form
  • Type typename is (list of elements)
  • Where
  • typename is the name given the type (e.g.
    gender_type)
  • list of elements is the list of type members
    (e.g. male, female)
  • Example
  • Type gender_type is (male, female)

6
Enumerated Types (cont.)
  • Enumerated types can be used in input and output
    by instantiating and using a predefined Ada
    package.
  • Package gender_io is new ada.text_io.enumeration_i
    o(enumgt gender_type)
  • Given that gender is of type gender_type, you may
    then use
  • gender_io.put(itemgtgender)
  • gender_io.get(itemgtgender)

7
Type Attributes
  • All data types have predefined attributes
  • typefirst is the first element of the type
  • typelast is the last element of the type
  • typepos(value) returns the position within the
    type of a given value
  • typeval(pos) returns the value in a particular
    position of the type
  • typepred(value) returns the predecessor of the
    given value within the type
  • typesucc(value) returns the successor of the
    given value within the type

8
Type Attributes (cont.)
  • Given the type declaration
  • Type group_type is (Mary, Bob, Carl, Kelly)
  • group_typefirst is Mary
  • group_typelast is Kelly
  • group_typeval(1) is Bob (note position numbers
    start with 0)
  • group_typepos(Carl) is 2
  • group_typepred(Bob) is Mary
  • group_typesucc(Carl) is Kelly
  • IMPORTANT pred of the first element and succ of
    the last element are undefined!!
Write a Comment
User Comments (0)
About PowerShow.com