Ada Cheat Sheet
Key Ada syntax covering strong typing, control flow, packages, and exception handling for safety-critical software.
Hello World
A minimal Ada procedure.
with Ada.Text_IO; use Ada.Text_IO;procedure Hello_World isbegin Put_Line ("Hello, World!");end Hello_World;
Types & Variables
Declaring variables and custom types.
declare X : Integer := 42; Y : Float := 3.14; Name : String := "Ada"; type Day is (Mon, Tue, Wed, Thu, Fri, Sat, Sun); -- enumeration type Percentage is range 0 .. 100; -- constrained subtype Today : Day := Wed;begin Put_Line ("X = " & Integer'Image (X));end;
Control Flow
Conditionals, loops, and procedures.
- if / elsif / else- if cond then ... elsif cond2 then ... else ... end if;
- for loop- for I in 1 .. 10 loop ... end loop; counts over a range
- while loop- while cond loop ... end loop; condition-checked loop
- case statement- case Day is when Mon => ... when others => ...; end case;
- loop with exit- loop ... exit when cond; end loop; explicit-exit loop
- procedure Name (Arg : Integer) is ... end Name;- Procedure declaration
Packages
Splitting specification and body across a package.
package Math_Utils is function Square (X : Integer) return Integer;end Math_Utils;package body Math_Utils is function Square (X : Integer) return Integer is begin return X * X; end Square;end Math_Utils;
Exceptions
Raising and handling errors.
- exception when Constraint_Error => ...- Handles a specific predefined exception
- raise Program_Error;- Explicitly raises an exception
- Ada.Exceptions- Package for inspecting/handling exception information
- others =>- Catch-all branch in an exception handler
- My_Error : exception;- Declares a user-defined exception
- begin ... exception ... end;- Block structure pairing handlers with a scope
Records & Variants
Composite types and discriminated variant records.
type Point is record X, Y : Integer := 0;end record;type Shape_Kind is (Circle, Rectangle);type Shape (Kind : Shape_Kind) is record case Kind is when Circle => Radius : Float; when Rectangle => W, H : Float; end case;end record;P : Point := (X => 1, Y => 2);C : Shape (Circle) := (Kind => Circle, Radius => 2.5);
Generics
Parameterized reusable units with generic.
generic type Element is private;procedure Swap (A, B : in out Element);procedure Swap (A, B : in out Element) is Tmp : constant Element := A;begin A := B; B := Tmp;end Swap;-- instantiateprocedure Swap_Int is new Swap (Integer);
Tasks & Protected Objects
Built-in concurrency with tasks and rendezvous.
task type Worker is entry Start (ID : Integer);end Worker;task body Worker is My_ID : Integer;begin accept Start (ID : Integer) do My_ID := ID; end Start; Put_Line ("Worker" & Integer'Image (My_ID));end Worker;protected Counter is procedure Inc; function Value return Integer;private N : Integer := 0;end Counter;
Type Attributes
Compile-time queries on types and objects.
- Integer'First / Integer'Last- smallest and largest value of a type
- T'Range- the index range of an array or discrete type
- A'Length- number of elements in array A
- T'Image (X)- string representation of value X
- T'Value (S)- convert string S back to a value of type T
- T'Succ (X) / T'Pred (X)- next or previous value in a discrete type
Subtypes & Contracts
Range constraints and Ada 2012 aspects.
- subtype Positive is Integer range 1 .. Integer'Last- constrained subtype checked at runtime
- type Percent is range 0 .. 100- new integer type with its own bounds
- with Pre => X > 0- precondition contract on a subprogram
- with Post => Result >= 0- postcondition on the returned value
- with Dynamic_Predicate => ...- arbitrary runtime constraint on a subtype
- with Type_Invariant => ...- invariant maintained on a private type
Lean on Ada's strong typing — define distinct subtypes with explicit ranges (type Percentage is range 0 .. 100) so the compiler catches out-of-range and unit-mixing bugs before runtime.