Ada is a high-level, statically typed programming language designed for reliability, maintainability, and safety. Originally developed in the late 1970s under a U.S. Department of Defense initiative, Ada is widely used in critical systems such as avionics, defense, and aerospace applications. This guide covers the basics of the Ada programming language to help beginners get started.
Ada Basics: A Comprehensive Guide for Beginners. |
Ada is a structured, statically typed, and block-structured programming language. It emphasizes readability, robustness, and reliability, making it ideal for high-integrity systems. It supports modular programming, strong typing, exception handling, and concurrency.
To start programming in Ada:
Example - Hello World:
with Ada.Text_IO; use Ada.Text_IO;
procedure Hello is
begin
Put_Line ("Hello, World!");
end Hello;
Explanation:
with
: Specifies libraries to include.use
: Makes library components available.Put_Line
: Outputs text followed by a newline.Variables and Types:
Name : String := "Alice";
Age : Integer := 25;
Height : Float := 5.8;
Is_Student : Boolean := True;
Ada uses strong typing, meaning variables must be explicitly declared with their types.
Constants:
PI : constant Float := 3.14;
Reading Input:
with Ada.Text_IO; use Ada.Text_IO;
procedure Input_Example is
Name : String(1..20);
begin
Put("Enter your name: ");
Get_Line(Name);
Put_Line("Hello, " & Name & "!");
end Input_Example;
Conditional Statements:
if Age >= 18 then
Put_Line("Adult");
elsif Age > 12 then
Put_Line("Teenager");
else
Put_Line("Child");
end if;
Loops:
-- For Loop
for I in 1..5 loop
Put_Line(Integer'Image(I));
end loop;
-- While Loop
Count : Integer := 0;
while Count < 5 loop
Put_Line(Integer'Image(Count));
Count := Count + 1;
end loop;
Arrays:
Fruits : array(1..3) of String := ("Apple", "Banana", "Cherry");
Put_Line(Fruits(1)); -- Access first element
Records (Structures):
type Person is record
Name : String(1..20);
Age : Integer;
end record;
John : Person := (Name => "John", Age => 30);
Put_Line(John.Name);
Defining Procedures:
procedure Greet(Name : String) is
begin
Put_Line("Hello, " & Name & "!");
end Greet;
Defining Functions:
function Square(X : Integer) return Integer is
begin
return X * X;
end Square;
Ada supports modular programming through packages.
Example:
package Math_Functions is
function Square(X : Integer) return Integer;
end Math_Functions;
package body Math_Functions is
function Square(X : Integer) return Integer is
begin
return X * X;
end Square;
end Math_Functions;
Example:
begin
Put_Line(Integer'Image(10 / 0));
exception
when Constraint_Error =>
Put_Line("Division by zero is not allowed.");
end;
Ada supports multitasking using tasks.
Example:
task type Worker is
entry Start;
end Worker;
-- Implementation
task body Worker is
begin
accept Start;
Put_Line("Task started.");
end Worker;
Writing to a File:
with Ada.Text_IO; use Ada.Text_IO;
File : File_Type;
begin
Create(File, Out_File, "output.txt");
Put_Line(File, "Hello, File Handling in Ada!");
Close(File);
end;
Reading from a File:
with Ada.Text_IO; use Ada.Text_IO;
File : File_Type;
Line : String(1..100);
begin
Open(File, In_File, "output.txt");
Get_Line(File, Line);
Put_Line(Line);
Close(File);
end;
Ada is a powerful and reliable programming language designed for safety-critical systems. Its focus on strong typing, modularity, concurrency, and error handling makes it ideal for applications requiring high levels of security and stability. With Ada's structured approach, developers can create robust and maintainable code for demanding projects.