Tuesday, February 9, 2016

Variables, Constants, and Arithmetic in Ada

Declaring and Initializing Variables

Variables in interpreted languages, not functional ones, are ways to store information for use in a program. To be clear, Ada is an interpreted language, and thus does use variables!

The syntax for creating (declaring) a variable is as follows:

name_of_variable : type_of_variable;

To assign a variable an initial value, we do:

name_of_variable : type_of_variable := value;

The " := " is how we set or assign variables to values. Let's say we want to initialize a variable a value and then change it later. We do:

myVariable: integer := 5;
begin
     myVariable := 6;
end

Notice how we can put the changing of the variable after the "begin" keyword. 

We can also use the declare keyword to declare variables. It would go something like this:

begin
     declare
          -- declarations

     begin
           --initializations

     end

end

Types of Variables and How to Display Them in Output

Here is a list of very common variables with examples of each and how you would display them in the Put_Line statement. The variable names of each in these examples of display syntax are "my"+Type. Replace it with your variable name.


            Type                           Examples                                         Display Syntax
integer
1, 2, 3
Put_Line(integer’image(myInteger))
float
5.1, 6.75, 14.5
Put_Line(float’image(myFloat))
String
“Hello there”, “Area = 80.”
Put_Line(myString)
character
‘M’, ‘a’
Put_Line(character’image(myCharacter))
Decimal_Number
5.1, 6.75, 14.5
Put_Line(Decimal_Number’image(myDecimal))
Natural_Number
1, 2, 3
Put_Line(Natural_Number’image(myNaturalNum))

As you can see, the display syntax of each variable is consistent:

For numbers: Type'image(variable)
For strings: just the variable.


Constants

Constants are unchangeable values. These can be declared by placing "constant" before the variable type when declaring AND initializing a variable. The syntax is as follows:

variableName : constant type := value;

NOTE: You MUST initialize constants.


Specifying Ranges:

Decimal_Number and Natural_Number


Range of certain variables can be specified using Decimal_Number and Natural_Number. to use these variables, we have to declare the precision of them.

The syntax is as follows for Natural_Number:

type Natural_Number is Range minNum .. maxNum;

where minNum and maxNum are integer values. This will create limits for how low or high a natural_number can be.

For Decimal_Number:

type Decimal_Number is digits numDigits;

where numDigits is an integer value. This will specify how many digits a decimal_number will have.

What do I mean by "number of digits"? Here's some examples:

5.002 has 4 digits whereas 52.1 has 3 digits.

This is a program to demonstrate Decimal_Number and Natural_Number:

The output:


As you can see, the digits displayed in number1 is exactly 3, the number of digits we specified in the precision statement.

Arithmetic in Ada

Ada uses fairly common operators for arithmetic.

The list:

+      Addition
-       Subtraction
*      Multiplication
/       Division
**    Exponents
&     Concatenation

How do we perform arithmetic with variables? There are one quite annoying rule that we have to follow in Ada:

If the variables are types of numbers, you can only do arithmetic with variables of the same type.

So, it is not possible to do an Integer + Float not a Natural_Number - Decimal_Number or a Decimal_Number * float .

Here is an example of calculating the area of a circle. It uses float point numbers, multiplication, constantsstrings, and string concatenation with those float variables to give us a nice message to explain the output:


The output of this code:

:


Here is another example of a simple calculation of a bunch of numbers.




And, the output of this code, telling us what numbers were involved in this calculation, the operators, and the result.










































































1 comment:

  1. Hey Nick this is Cameron I will be viewing your ada blog. SO far it seems like an interesting language to learn.

    ReplyDelete