# Declarations, Integers, Naturals, Attributes, I/O, Operators

## Declarations, Integers, Integer Output

### Declare and Print Some Integers

• File `some_ints.adb`
• ```   with Ada.Text_IO; use Ada.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

• File `some_ints.adb`
• ```   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(Integer'image(i));
put_line(Integer'image(j));
end images;
--| 11
--|-22
--| 11
--|-22
```
• 'img and 'image - like to_string - convert integer to String

• 'img and 'image are attributes

### 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;
```

### (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
• 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;
...
```

### 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
1. `**, not, abs `
2. `*, /, mod, rem`
3. `+, -` (unary)
4. `+, -,` &
5. `=, /=, < <=; > >=`
6. `and, or, xor`
• Membership test and short circuit:
1. `in` - Membership test
2. `and then, or else` (short circuit)

• Notes:
• words: `not, abs, mod, rem, and, or, xor`
• Exponentiation: 2 ** 3 = 8, 2 ** -3 = 0.125 = 18
• 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