Whirlwind Tour of Ada - Part 3a
Structured Statements
Input of Integers and Characters
getcountpzn.adb
- getcountpzn.adb and prettified
- Reads a specified number of integers and counts the positives, zeros, and
negatives in the input
- Demonstrates:
- Subtype Natural, constant, multiple declarations
- if statements, for loops
- integer
get
procedure
Subtype Natural
- Variables of type Natural holds values in the range 0 .. Integer'last
- Use type Natural for counting
- Natural is a subtype of the integers: it is a subset of the values in the type
Integer
- Compiler allows a variable of type Natural anywhere a variable of type
Integer can be used, and vice versa
- Examples - these compile:
i,j: Integer;
n: Natural
...
i := n;
n := j;
FYI: here's how Natural is declared:
subtype Natural is 0 .. Integer'Last;
Constants and Multiple Declarations
- Constants must be initialized and can't appear on the
left of an assignment
- Can assign values to multiple variables in a single declaration
For Loops
More on For Loops
- Other increments: Some options
- Use a while loop
- Increment another variable in a for loop
- Reverse loop: output 10, 9, ..., 1
for i in reverse 1 .. 10 loop
put(i);
end loop;
Can have an empty range (eg for i in low .. hi, where low
> hi)
If Statements
- Parens not needed around condition
- Explicit
then
keyword
- Syntax:
if ... elsif ... else ... end if
-
elsif
and else
are optional
- Can have 0 or more
elsif
- Can have 0 or 1
else
-
End if
required, even if body has one statement
Compound Parts of If Statements
- Compound parts don't need explicit brackets
- Count grades in Ada:
if g >= 90 then
put_line("found an A");
numA := numA + 1;
elsif g > 80 then
put_line("found an B");
numB := numB + 1;
elsif g > 70 then
put_line("found an C");
numC := numC + 1;
else
put_line("found another");
numOther := numOther + 1;
end if;
In java compound parts require brackets:
if (g >= 90){
S.o.p("found an A");
numA++;
}
else if (g >= 80){
S.o.p("found a B");
numB++;
}
else if (g >= 70){
S.o.p("found a C");
numC++;
}
else {
S.o.p("found another grade");
numOther++;
}
Java we use else if
instead of elsif
Contrast elsif and else if
- An if is a single statement, and so it can be used in the else part of a Java if
statement without brackets
- What would Java look like if brackets were required:
if (g >= 90){
S.o.p("found an A");
numA++;
}
else { if (g >= 80"){
S.o.p("found a B);
numB++;
}
else { if (g >= 70"){
S.o.p("found a C);
numC++;
}
else {
S.o.p("found another grade");
numOther++;
}}} // Careful with the braces!
Or like this:
if (g >= 90){
S.o.p("found an A");
numA++;
}
else {
if (g >= 80"){
S.o.p("found a B);
numB++;
}
else {
if (g >= 70"){
S.o.p("found a C);
numC++;
}
else {
S.o.p("found another grade");
numOther++;
}
}
} // Careful with the braces!
Languages that require closing brackets tend to use elsif
- Examples: Ada and perl use elsif; python uses elif
Case Statement
case i is
when 1 =>
...
when others =>
...
end case;
Must account for each value in type of i
Ada.Integer_text_io.get
-
Ada.Integer_Text_io.get(v)
reads an integer from standard input and stores the value of that integer into variable
v
- Ada.Integer_Text_io.get is needed to read an integer, as an integer (as opposed to
a string)
-
Get
reads from standard input (see below for more info on
standard input)
- A call to
Get
skips white space until it finds an integer (or
an error occurs)
- Exceptions than can occur with a call to get:
- In a numeric value that is out or range is found, a CONSTRAINT_ERROR occurs
- If a non-numeric character is found, a DATA_ERROR exception occurs.
- If eof is reached before a number is found, an END_ERROR exception occurs.
Standard Input
- Statement get(x) reads from standard input
- Standard input normally reads from the keyboard
- To read from a file, there are two options:
- Redirect the input (see below)
- Explicitly declare and open the file and name it in the get statement
- Example:
get(myfile, x);
- Discussed later
Redirecting Standard Input
- Standard input can be redirected to read from a file
- Example: running the program like this
getcountpzn < mydata.txt
will cause a get from standard
input to read from the file mydata.txt
instead of from the keyboard.
Testing for End of File
- Test for end of file with Ada.text_io.end_of_file
- Example showing basic structure for testing for end of file:
with ada.text_io; use ada.text_io;
with ada.integer_text_io; use ada.integer_text_io;
procedure eofloop is
i: Integer;
begin
while not ada.text_io.end_of_file loop
get(i);
put(i);
new_line;
end loop;
end eofloop;
Example shown in next program
geteofcountpzn.adb
- geteofcountpzn.adb and prettified
- Counts positive, zero, and negative integers, until end of file is reached
- Demonstrates while loop and the end of file test:
While loop
- Things to note:
- No parens
- Loop structure: loop ... end loop;
Interactive I/O and End of File
- When doing interactive I/O, end_of_file will return true when you enter
- control-D (linux and unix)
- control-Z [on a line by itself] (windows)
- In interactive I/O, there is no file, but the I/O routines are written to treat
the special symbols as EOF
Interactive I/O: Using Prompts and EOF
- What happens if we add a prompt?
- getinteofcountpzn.adb and prettified
- What's happening:
- The EOF test must examine the input to test for EOF
- In interactive I/O, it must read an entire line to test for EOF
- How to solve???
General Loops, Sentinal Loops, Loop and a Half
- Let's solve the same problem with a general loop
- getgeninteofcountpzn.adb and prettified
- Let's try a sentinal loop instead of an EOF loop
- Write a procedure that echos positive numbers and halts on non-positive
- This is called a loop and a half
- Sequence: input, test, process, input, test, process, ...
Character Input
- If c is declared as
c: Character
, then get(c)
reads
exactly one character.
- Example program (prettified)
- The example inputs and outputs characters and inputs an integer.
- The output is a line containing the following:
a single character, a character repeated a given number of
times, and a final character.
- If the final character is a 'u', then all of the characters are output in upper case.
- Input specification:
- Specification for each input line is as follows:
- a character that is the first character to output
- the number of times to repeat the middle character
- a character that is the character to output in the middle
- a character that is the last character to output (and the key for upper case output)
- The last character also specifies whether to convert output to upper case
- The program runs until end of file
- Sample input:
A 11bc
1 11b2
1 11 2
a 11bu
a11bx
The input file above will produce the folowing output:
Abbbbbbbbbbbc
1bbbbbbbbbbb2
1 2
ABBBBBBBBBBBU
abbbbbbbbbbbx
done
Execution: If the data is in repeat_middle.txt
, then run with
repeat_middle < repeat_middle.txt