In this section, we will explore the various operators available in Ada and how to use them to form expressions. Operators are special symbols that perform operations on variables and values. Expressions are combinations of variables, constants, and operators that are evaluated to produce a result.

Types of Operators

Ada supports several types of operators:

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operators
  5. Miscellaneous Operators

  1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

Operator Description Example
+ Addition A + B
- Subtraction A - B
* Multiplication A * B
/ Division A / B
mod Modulus A mod B

Example:

with Ada.Text_IO; use Ada.Text_IO;

procedure Arithmetic_Operators is
   A : Integer := 10;
   B : Integer := 3;
   Result : Integer;
begin
   Result := A + B;
   Put_Line("A + B = " & Integer'Image(Result));
   
   Result := A - B;
   Put_Line("A - B = " & Integer'Image(Result));
   
   Result := A * B;
   Put_Line("A * B = " & Integer'Image(Result));
   
   Result := A / B;
   Put_Line("A / B = " & Integer'Image(Result));
   
   Result := A mod B;
   Put_Line("A mod B = " & Integer'Image(Result));
end Arithmetic_Operators;

  1. Relational Operators

Relational operators are used to compare two values.

Operator Description Example
= Equal to A = B
/= Not equal to A /= B
< Less than A < B
<= Less than or equal to A <= B
> Greater than A > B
>= Greater than or equal to A >= B

Example:

with Ada.Text_IO; use Ada.Text_IO;

procedure Relational_Operators is
   A : Integer := 10;
   B : Integer := 3;
   Result : Boolean;
begin
   Result := A = B;
   Put_Line("A = B: " & Boolean'Image(Result));
   
   Result := A /= B;
   Put_Line("A /= B: " & Boolean'Image(Result));
   
   Result := A < B;
   Put_Line("A < B: " & Boolean'Image(Result));
   
   Result := A <= B;
   Put_Line("A <= B: " & Boolean'Image(Result));
   
   Result := A > B;
   Put_Line("A > B: " & Boolean'Image(Result));
   
   Result := A >= B;
   Put_Line("A >= B: " & Boolean'Image(Result));
end Relational_Operators;

  1. Logical Operators

Logical operators are used to combine multiple conditions.

Operator Description Example
and Logical AND A and B
or Logical OR A or B
not Logical NOT not A

Example:

with Ada.Text_IO; use Ada.Text_IO;

procedure Logical_Operators is
   A : Boolean := True;
   B : Boolean := False;
   Result : Boolean;
begin
   Result := A and B;
   Put_Line("A and B: " & Boolean'Image(Result));
   
   Result := A or B;
   Put_Line("A or B: " & Boolean'Image(Result));
   
   Result := not A;
   Put_Line("not A: " & Boolean'Image(Result));
end Logical_Operators;

  1. Assignment Operators

Assignment operators are used to assign values to variables.

Operator Description Example
:= Assignment A := B

Example:

with Ada.Text_IO; use Ada.Text_IO;

procedure Assignment_Operators is
   A : Integer;
   B : Integer := 10;
begin
   A := B;
   Put_Line("A := B: " & Integer'Image(A));
end Assignment_Operators;

  1. Miscellaneous Operators

Ada also provides some miscellaneous operators.

Operator Description Example
& Concatenation "Hello" & " World"

Example:

with Ada.Text_IO; use Ada.Text_IO;

procedure Miscellaneous_Operators is
   Str1 : String := "Hello";
   Str2 : String := " World";
   Result : String;
begin
   Result := Str1 & Str2;
   Put_Line("Str1 & Str2: " & Result);
end Miscellaneous_Operators;

Practical Exercises

Exercise 1: Arithmetic Operations

Write a program that takes two integers as input and performs all arithmetic operations on them.

Solution:

with Ada.Text_IO; use Ada.Text_IO;

procedure Arithmetic_Exercise is
   A, B : Integer;
   Result : Integer;
begin
   Put("Enter first integer: ");
   Get(A);
   Put("Enter second integer: ");
   Get(B);
   
   Result := A + B;
   Put_Line("A + B = " & Integer'Image(Result));
   
   Result := A - B;
   Put_Line("A - B = " & Integer'Image(Result));
   
   Result := A * B;
   Put_Line("A * B = " & Integer'Image(Result));
   
   Result := A / B;
   Put_Line("A / B = " & Integer'Image(Result));
   
   Result := A mod B;
   Put_Line("A mod B = " & Integer'Image(Result));
end Arithmetic_Exercise;

Exercise 2: Logical Operations

Write a program that takes two boolean values as input and performs all logical operations on them.

Solution:

with Ada.Text_IO; use Ada.Text_IO;

procedure Logical_Exercise is
   A, B : Boolean;
   Result : Boolean;
begin
   Put("Enter first boolean (True/False): ");
   Get(A);
   Put("Enter second boolean (True/False): ");
   Get(B);
   
   Result := A and B;
   Put_Line("A and B: " & Boolean'Image(Result));
   
   Result := A or B;
   Put_Line("A or B: " & Boolean'Image(Result));
   
   Result := not A;
   Put_Line("not A: " & Boolean'Image(Result));
end Logical_Exercise;

Summary

In this section, we covered the various types of operators in Ada, including arithmetic, relational, logical, assignment, and miscellaneous operators. We also provided practical examples and exercises to help reinforce the concepts. Understanding these operators and how to use them in expressions is fundamental to programming in Ada. In the next section, we will delve into control structures, which allow you to control the flow of your programs.

© Copyright 2024. All rights reserved