Ada Fundamentals - Hello World and Table of Squares
Overview
- Ada Language Design Goals
- The Main Routine
- Comments
- Program Structure
- Library Ada.Text_io
- With and Use Statements
- Identifiers and Case Statements
- Literals
- Table of squares
- Assignment
- Assignment in C and Java
- Initialization
- Constants
Ada Langauge Design Goals
- Ada was designed with three primary goals:
- Reliability and Maintainability
- Concern for Programmer
- Efficiency (size and execution speed)
- As we learn the language, we will see how it supports these goals
Example 0: sayhi.adb
-- Simple Ada program
-- in file sayhi.adb
with Ada.Text_IO;
procedure sayHi is
begin
Ada.Text_IO.put("Hi!"); -- Output some text!
end sayHi;
What's in a Name - Methods, Procedures, and the Main Routine
- In Java, to run a program you execute a
- method
- named
main
- that is nested in a class
- In Ada, to run a program you execute a
- procedure
- that can have any name that you want
- and that is not nested inside anything
- Procedures and methods are similar
Comments
- Comments are marked with
--
to end of line
- Only one kind
- No multiline comment
- Why? What happens here in Java?
/* This is a comment * /
int x = a * b; // What about this line?
/* Another comment */
- Less of a problem with IDEs
Program Structure
Package Ada.Text_IO
-
ada.text_io
: package that contains
routines for i/o of text
- put_line: procedure for string output
- Outputs newline after string
- Some other routines in Ada.Text_IO:
- put: Outputs a string, without a newline
- new_line: outputs a newline character
- new_line(3): outputs 3 newline characters
With Statement
-
with Ada.Text_IO
statement: allows
procedure sayHi
to
refer to routines in package Ada.Text_IO
- If a routine accesses something that is in a package,
the routine must have a
with
statement for that package
-
With
is different from Java
import
, which causes the Java compiler to
automatically search available classes for a feature
Use Statement
with ada.text_io;
use ada.text_io;
procedure sayMore.adb is
begin
put_line("Hi");
put("Good to ");
put("see you friends!");
new_line;
end sayMore;
Use statement allows access to routines without using the
fully qualified name
- Compiler will automatically search ada.text_io for
put_line, put, and new_line
- with statement: tells compiler that the package will be used
- use statement: tells compiler that the package should be
automatically searched, and that the fully qualified name
is not required, although it can be used
Similar to Java import
Make sure you understand the difference between
with
and use
Identifiers and Case Sensitivity
- sayHi and sayMore: procedure names chosen by user
- Identifiers: begin with letters and have any number of
letters, digits, and underscores (underscores must appear
between two non-underscores)
-
Ada 95 Reserved words
-
Ada 05 Reserved words
- For comparison, here are Java's reserved words
- Reserved words cannot be used as identifiers
- Ada is NOT case sensitive:
- Example: sayhi, sayHi and SAYHI all refer to
the same variable
- Conventions:
- Variable names: camel notation (eg someName)
- Procedures and functions: camel notation (eg
someProc)
- Types: capitalized (eg Integer)
- Packages: capitalized (eg Ada.Text_IO)
- Warning: Operating systems can be case sensitive,
so watch file names
- For gnatmake, file name must be same as procedure name,
but must be all lower case: sayhi.adb, not sayHi.adb
Literals
- A Literal is a value that is
expressed directly in a program
- Example: In the expression (2 + 3), the "2" and the "3"
are literals that represent the obvious values.
The expression itself represents the value 5, but the
literal "5" does not itself appear in the expression.
- String literal: enclosed in double quotes (eg "Hi Mom!", "")
- Character literal: enclosed in single quotes (eg 'H')
- Numeric Literals
- Can contain underscores (eg
1_000_000_000,
1_000.000_1
)
- Underscores must be between 2 digits
- Floating point numbers require digits on each side of decimal
point (eg 0.1, not .1 and not 0.
- Bases 2 to 16:
- binary:
2#0011_1010#
- ternary:
3#0021_1210#
- octal:
8#0711_1710#
- decimal:
10#9281_1928#
- hexadecimal:
16#3A#
- Exponents are represented using letter E:
10E3
is 10_000
1.0E3
is 1_000.0
2#110#E5
is 6 * 2**5
- Enumerated types
- Later we will see how to create new literals for a new type
- Example:
type color is (red, blue, green);
c: color := blue;
Some Java literals: Numbers.java
Example 1: Table of Squares and Cubes
- This program prints a table of squares and cubes of the
integers from 2 to 10: table1.adb
(prettified version)
- It shows the following concepts:
- if and for loop statements
- Variable declaration
- Assignment
- Integer output
Assignment Statement
- Assignment symbol is
:=
- Equality represented by
=
, as in math
if a = b then
c := d;
elsif e /= f then
g := h;
end if;
Avoids assignment errors found in C/Java family
Assignment in Java and C
boolean a = false;
boolean b = true;
if (a = b)
System.out.println("Same");
else
System.out.println("Different");
What about C?
i = 3;
j = 4;
if (i = j)
printf("Same\n");
else
printf("Different\n");
Assignment Operator?
- In C/Java, an assignment operator returns a value.
-
The Ada assignment symbol is not an operator and does not return a value.
Initialization
- The initial value of a variable is the current contents of its memory cell, unless it is initialized!
with ada.integer_text_io;
procedure initialize is
data1: Integer;
begin
put(data1);
end initialize;
- Outputs whatever value happens to be in memory at that location
- No definite assignment as in java.
- Watch out for subtle errors here: what happens to be in memory can
vary from compiler to compiler and run to run!
Constants
- An identifier that is declared to be a constant
- must be initialized
- cannot appear on the left side of an assignment
- Example:
procedure constants is
data1: Integer;
data2: Constant Integer := 2;
-- data3: Constant Integer; -- Compile error
begin
data1 := 1;
-- data2 := 2; -- Compile error
end initialize;
Is this true in Java?