Ada Cheatsheet

2644




Data types

  • Strongly typed
TypesData-type
BasicInteger, Character, Float, Long_Float, Short_Integer, Long_Integer, etc
Pointeraccess
Compositearray, String
User Defined Data Typerecord, enumeration, tagged type, variant record, range types, etc.

Naming convention

  • First character in identifier is a letter, then letters (both uppercase and lowercase letters), digits and underscore(_).
  • Case insensitive

Arrays

array-name : array (Fist..Last) of element-type; -- one-dimensional Array
array-name : array (Fist1..Last1, First2..Last2) of element-type; -- two-dimensional Array

String

It is an array of Character.

My_String := String := "abcde"; -- string declaration

Literals or Constants

LiteralExample
Integer Literal- decimal255
Integer Literal- octal8#377#
Integer Literal- hexadecimal16#FF#
Float point Literal53.0, 79e-6
Character literals'a', '1'
String literals"OneCompiler", "Foo"
Boolean literalsTrue, False

Special characters

Special characters are defined as constans in ASCII or Ada.Characters.Latin_1 package.

ConstantDescription
LFNew line
CRCarriage Return
HTHorizontal tab
VTVertical tab
NULNull character

Operators

Operator typeDescription
Arithmetic Operator+ , - , * , / , ** (exponentiation)
Relational Operator< , > , <= , >=, /= , =
Logical Operatorand , or, xor, not
Assignment Operator:=
If expression(if a then b else c)

Sample program

with Ada.Text_IO; use Ada.Text_IO;
procedure Hello is
begin
   Put_Line ("Hello, World!");
end Hello;
  • Ada.Text_IO : package that contains routines for I/O of text.
  • with : tells compiler that the package will be used.
  • use : tells compiler that the package should be automatically searched and allows access to routines without using the fully qualified name.
  • procedure : used to declare a procedure named Hello here.
  • Put_Line : is a procedure for string output.
  • -- : comment.

Variables

varName : DataType := value;

Constants

varName : constant DataType := value;

Conditional Statements

1. If-else

if conditional-expr then 
    --code if above statement is true
elseif conditional-expr then 
    --code if above statement is true
end if;

2. Case

case expr is 
    when expr => --code
    when expr => --code
    ...
    when others => --code;
end case;

Loops

1. Infinite loop:

This is the simplest loop

   Index := 1; --initialization
   loop                            
      --code
      exit when Index = n;
   end loop;

2. while loop

   Count := 1;    --initialization
   while Count < n loop  
      --code
   end loop;

3. for loop

for Index in 1..n loop          
   --code
end loop;

Sub programs

Functions and procedures are collectively called as sub-programs.

Syntax for procedures

procedure proc_name
   (X : in Integer ; Y : out Integer ; Z : in out Integer ) is
begin
   X := 10; −− it's an Error as you can’t modify an in parameter.
   Y := X; −− can modify Y as it's an out parameter.
   Z := Z + 1; −− can read and write as it's an in out parameter.
end proc_name;

Syntax for functions

function function_name(parameter : parameter_type) return value is
begin
   --code
end function_name;