home—lectures—textbook—exams—hws—D2L—breeze (snow day)
ada syntax intro
Go over features in:
hw01.html
-
We use text_io, and float_text_io, and integer_text_io;
in java, everything was coerced to String
-
Although String looks just like java strings,
they act very different. They are arrays, which means:
-
they are mutable
-
but their length is fixed
-
String-arrays are indexed from 1 (by default), not 0.
-
What if you print a var who is uninitialized?
A: whatever is in memory
(not 'nothing', not 'null', not an address, not (necess.) 0.0.)
-
fewer explicit casts than in Java (in fact, nearly none) .
Ada requires types to match for `:=`, arith.ops and func.calls
Note how Java's double d = 3; involves an implicit cast,
and float f = 3.0; is a compile-error.
-
(when showing call-by-keyword) -- terms "actual argument" and "formal parameter"
(vs call-by-position)
More example programs:
www.radford.edu/nokie/classes/320/declareints.html
Some Loops
We'll have more to say about loops, but here are three ways to count from 10 through 20:
- while-loop (Java-like)
i: Integer := 10;
while i<=20 loop
put(i);
i := i+1;
end loop
put("after the loop, i=");
put(i); -- i=21
|
- for-loop (Java-like, except not)
for i in range 10..20 loop
put(i);
end loop;
-- `i` is out of scope here; before disappearing, it's last value was 20, not 21.
|
- exit-when loop
i: Integer := 10;
loop
put(i);
exit when i=20;
i := i+1;
end loop
put("after the loop, i=");
put(i); -- i=20
|
This could be achieved in Java with a while(true){ ...; if .. break; ...},
but feels cleaner and more idiomatic.
Definition: an (abstract data) Type is
a set of values with a set of operations.
Operators: glance at
operators and expressions
and note:
-
Six levels of precendence
chosen as a judicious balance between
Java/C (about 13 levels of precedence for 35+ operators),
and racket (no precedence or operators,
but parentheses always required).
-
There are
and,or
as well as
and then,or then!!
The difference is that the latter short-circuits.
(It is rare that one does not want short-circuiting:
it often is required for correctness (e.g. don't divide by 0),
and your boolean expressions should not have side-effects
like if (y>3 || z++>y) { … }!)
-
Not only do and and or have the same
level-of-precedence,
but you can't mix/match them
without specifying parentheses.
- xor is actually used,
because we remember its name.
Note that it's the same as /= for booleans,
but more readable/understandable.
-
The name /= is particularly intuitive (cf. “≠”)
More on ranges
We have seen ranges used in loops, and in
subtypes. (remind).
They can also be used with the “in” operator!:
if n in 10..20 then …
workwork : Boolean := day in Mon..Fri and not federalHoliday;
|
Still not cool enough?
Multiple ranges can be combined with
the “|” operator as shown:
if n in 10..20|99|-12..-6 then …
|
Note:
Example of put mentions that
we can't mix positional *after* a keyword;
Note:
call by fully-resolved name: `Ada.Integer_Text_IO.put( "hello" );` .
This statement is also a type-error,
and it demonstrates that
the error message, when involves IO libraries, is kinda long;
the essence of the message is a bit buried.
home—lectures—textbook—exams—hws—D2L—breeze (snow day)