This chapter should hopefully have provided no nasty surprises. If you had been given a piece of real Ada code before we started you should have been able to pick out the control structures, for example, without too many problems.
The following list extracts the statement definitions from the above section and acts as a quick reference to the syntax introduced so far. Italic items are optional in the syntax.
-- Null Statement null; -- Compound Statement block_name: declare declarations begin statements exception exception_handler endblock_name; -- If Statement if expression then statements elsiexpression_1 and then expression_2 or else expression_3 then statements else statements end if; -- Case Statement case expression is when value => statements when value_1 .. value_2 => statements when value_1 | value_2 => statements when others => statements end case;-- Loop Statement Loop_Name: loop statements end loop Loop_Name; -- While Loop Loop_Name: while expression loop statements end loop; -- For Loop Loop_Name: for ident in reverse range loop statements end loop; -- Exit Statement Loop_Name: loop exit Loop_Name when expression; end loop; -- Return Statement; return value; -- Label and Goto <<label>> goto label; -- Raise an exception raise Invalid_Operator; -- Subprogram Specification function func_name(parameters) return return_type; function "+"(parameters) return Integer; procedure proc_name(parameters); procedure proc_name; -- Parameter types procedure proc_name(Param_1 : in Integer; Param_2 : out Integer; Param_3 : in out Integer; Param_4 : in Integer := 1); -- Subprogram body function func_name(parameters) return return_type is declarations begin statements exception when ident => statements when ident | ident => statements when others => statements end func_name;
What I have attempted to provide in this chapter is a basic grounding in Ada syntax and an insight into some of the language style. The goal of readability is helped by the provision of block naming, parameter naming and the use of keywords such as begin and end instead of { and }. Safety and predictability is enhanced with a simple and well understood exception mechanism, which although not as flexible as its C++ counterpart can be used as effectively. Parameter modes specified by in and out are preferable to relying on convention and passing of pointers.
Example 2.5 - Some C++/Ada mixed code.
This example shows the common errors made by programmers working with C/C++ and Ada. They are simply typing errors which make sense to a C compiler, see if you can spot them all (answers in the file Fixed.adb).
Copyright ©
1996 Simon Johnston &
Addison Wesley Longman