Content deleted Content added
Tags: Reverted references removed |
m Reverted edits by 170.177.225.123 (talk) to last version by Citation bot |
||
Line 148:
<syntaxhighlight lang="bash">gnatmake hello.adb</syntaxhighlight>
=== Data types ===
Ada's type system is not based on a set of predefined [[primitive types]] but allows users to declare their own types. This declaration in turn is not based on the internal representation of the type but on describing the goal which should be achieved. This allows the compiler to determine a suitable memory size for the type, and to check for violations of the type definition at compile time and run time (i.e., range violations, buffer overruns, type consistency, etc.). Ada supports numerical types defined by a range, modulo types, aggregate types (records and arrays), and enumeration types. Access types define a reference to an instance of a specified type; untyped pointers are not permitted.
Special types provided by the language are task types and protected types.
For example, a date might be represented as:
<syntaxhighlight lang="ada" line>
type Day_type is range 1 .. 31;
type Month_type is range 1 .. 12;
type Year_type is range 1800 .. 2100;
type Hours is mod 24;
type Weekday is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);
type Date is
record
Day : Day_type;
Month : Month_type;
Year : Year_type;
end record;
</syntaxhighlight>
Important to note: Day_type, Month_type, Year_type, Hours are incompatible types, meaning that for instance the following expression is illegal:
<syntaxhighlight lang="ada" line>
Today: Day_type := 4;
Current_Month: Month_type := 10;
... Today + Current_Month ... -- illegal
</syntaxhighlight>
The predefined plus-operator can only add values of the same type, so the expression is illegal.
Types can be refined by declaring [[subtyping|subtypes]]:
<syntaxhighlight lang="ada" line>
subtype Working_Hours is Hours range 0 .. 12; -- at most 12 Hours to work a day
subtype Working_Day is Weekday range Monday .. Friday; -- Days to work
Work_Load: constant array(Working_Day) of Working_Hours -- implicit type declaration
:= (Friday => 6, Monday => 4, others => 10); -- lookup table for working hours with initialization
</syntaxhighlight>
Types can have modifiers such as ''limited, abstract, private'' etc. Private types do not show their inner structure; objects of limited types cannot be copied.<ref>{{cite web |title=Ada Syntax Card |url=http://www.digilife.be/quickreferences/QRC/Ada%20Syntax%20Card.pdf |access-date=28 February 2011 |url-status=dead |archive-url=https://web.archive.org/web/20110706133825/http://www.digilife.be/quickreferences/QRC/Ada%20Syntax%20Card.pdf |archive-date=6 July 2011}}</ref> Ada 95 adds further features for object-oriented extension of types.
=== Control structures ===
Ada is a [[structured programming]] language, meaning that the flow of control is structured into standard statements. All standard constructs and deep-level early exit are supported, so the use of the also supported "[[goto (command)|go to]]" commands is seldom needed.
<syntaxhighlight lang="ada" line>
-- while a is not equal to b, loop.
while a /= b loop
Ada.Text_IO.Put_Line ("Waiting");
end loop;
if a > b then
Ada.Text_IO.Put_Line ("Condition met");
else
Ada.Text_IO.Put_Line ("Condition not met");
end if;
for i in 1 .. 10 loop
Ada.Text_IO.Put ("Iteration: ");
Ada.Text_IO.Put (i);
Ada.Text_IO.Put_Line;
end loop;
loop
a := a + 1;
exit when a = 10;
end loop;
case i is
when 0 => Ada.Text_IO.Put ("zero");
when 1 => Ada.Text_IO.Put ("one");
when 2 => Ada.Text_IO.Put ("two");
-- case statements have to cover all possible cases:
when others => Ada.Text_IO.Put ("none of the above");
end case;
for aWeekday in Weekday'Range loop -- loop over an enumeration
Put_Line ( Weekday'Image(aWeekday) ); -- output string representation of an enumeration
if aWeekday in Working_Day then -- check of a subtype of an enumeration
Put_Line ( " to work for " &
Working_Hours'Image (Work_Load(aWeekday)) ); -- access into a lookup table
end if;
end loop;
|