Declarations, Integers, Naturals, Attributes, I/O, Operators
Declarations, Integers, Integer Output
Declare and Print Some Integers
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
-- Integer I/O library
procedure some_ints is
i, j: Integer;
k: Integer := 333;
c: constant Integer := 4444;
begin
i := 1;
j := 22;
put(i); new_line; -- Default minimum width 12
put(j); new_line; -- Default minimum width 12
put(k, 5); new_line; -- Minimum width 5
put(c, 1); new_line; -- Minimum width 1
end some_ints;
--| 1
--| 22
--| 333
--|4444
Integer IO library
Declaration section
variable: type
:= for assignment (= is for equality)
constant
default width 12
minimum width
no put_line for integers
Attributes, Subtypes Natural and Positive
Attributes 'img and 'image
with Ada.Text_IO; use Ada.Text_IO;
procedure images is
i: Integer := 11;
j: Integer := -22;
begin
put_line(i'img);
put_line(j'img);
put_line(i'image);
put_line(j'image);
put_line(Integer'image(i));
put_line(Integer'image(j));
end images;
--| 11
--|-22
--| 11
--|-22
--| 11
--|-22
'img and 'image - like to_string - convert integer to String
var'img and var'image - not yet standard
'img and 'image are attributes
'img and 'image not allowed for structured types (eg arrays)
- Ada 2020 extends 'image for (almost) all types
More recent versions of Ada allow i'image
More Attributes: 'value, 'first, 'last, 'size
- Variables and types have attributes
- Type attribute:
Integer'image(i)
- Variable attribute:
i'img;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure attributes is
i: Integer := Integer'value("999");
begin
put(i); -- 999
put(Integer'first); -- smallest Integer
put(Integer'last); -- largest Integer
put(i'size); -- 32 (32 bit integers)
end attributes;
What is the inverse of 'value?
(Sub)Types Natural and Positive
- Use Subtypes Natural and Positive if possible
- Benefit: Catch errors earlier
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure nat_pos is
Number_Of_Students: Natural := 0;
Year: Positive := 2017;
begin
put(Natural'first); -- What is this?
put(Natural'last); -- What is this?
Number_Of_Students := Number_Of_Students - 1;
-- Meant to write + 1
end nat_pos;
Later we learn that Natural and Positive are subtypes of Integers
- Subtype = subset of values, same operations
- Thus, Integers, Naturals, Positives can be mixed freely:
stupid: Integer := Number_Of_Students;
weird: Positive := Number_Of_Students + Year;
weirder: Natural := Number_Of_Students - Year;
...
Ada.Integer_Text_IO.put(Year);
Another Attributes: 'range
- Another attributes: 'range
procedure nat_pos is
i: Integer;
Number_Of_Students: Natural := 0;
Year: Positive := 2017;
begin
...
if i in Natural'first .. Natural'last then ...
if i in Positive'range then ...
-- Positive'range is Positive'first .. Positive'last
for j in My_Array'range loop ...
end attributes;
'range is VERY useful for arrays and loops!
Expressions and Operators
Expressions and Operators
- Operators - high to low precedence
-
**, not, abs
-
*, /, mod, rem
-
+, -
(unary)
-
+, -,
&
-
=, /=, < <=; > >=
-
and, or, xor
- Membership test and short circuit:
-
in
- Membership test
-
and then, or else
(short circuit)
- Notes:
- words:
not, abs, mod, rem, and, or, xor
- Exponentiation: 2 ** 3 = 8, 2 ** -3 = 0.125 = 1⁄8
- Absolute value: (eg
abs
)
- Integer division: (eg
9 / 2 = 4
)
- Mod and rem differ only for negative operands
- Clock arithmetic:
(2 - 4) mod 12 = 10. (2 - 4) % 12 == -2.
- & is string concatenation:
put_line("Hi " & "Mom!");
-
put_line("Hi " & "Mom!");
-
put_line("x = " & x'img);
- /=
-
xor
- one or the other but not both
-
and, or, xor
all at same level
- Parens required if mixed:
x = y and (a
= b or c = d);
- Membership test (
if x in Natural then)
- Short circuit example:
if y /= 0 and then x / y > z then
- Only evaluate second operand if needed
- Java??
Integer Input - Different from Java!
Integer Input!
- Similar to - but different - from
Scanner s = new Scanner(System.in); i = s.nextInt();
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure get_int is
i, j, sum: Integer;
begin
get(i);
get(j);
sum := i + j;
put(sum);
end get_int;
Get(i)
modifies the value of i
Get(i)
skips white space until it finds an integer