Ada Fundamentals - Structured Statements
Overview
- Loops
- For Loops
- While Loops
- General Loops
- Loop and a Half
- Named Loops
- Selection Statements
- Some Java Errors
For Loop Statement
for i in start .. finish loop
-- loop body here
end loop;
Execute loop for each value in range range start .. finish
- if start = finish, execute 1 time
- if start > finish, execute 0 times
No brackets needed: Mark end of loop with end loop;
-
end loop;
is REQUIRED even if the loop
body contains only one statement
Loop variable does not need to be declared
- Loop variable not visible outside loop
- Loop variable hides variable with same name (Not
recommended)
Loop variable cannot be modified
What does this do: for i in reverse 1 .. 10 loop
What happens here: JavaFor.java
- What does the Ada version look like?
Three Kinds of Loops
for i in 1 .. 10 loop
...
end loop;
While loop
i := 1;
while i <= 10 loop
...
i := i + 1;
end loop;
General loop (infinite loop that can have an exit)
i := 1;
loop
exit when i = 10;
...
i := i + 1;
end loop;
Loop and a Half
- Is exiting from the middle of a loop a good idea?
- How to code input, test, process, input, test, process, input, ...
- Contrast this:
input i
while i /= flagValue loop
process i
input i
end loop;
With this:
loop
input i
exit when i = flagValue;
process i
end loop;
Which is shorter, clearer, more error prone
Guidelines for using exit:
- Use exit when rather than if C then exit
- If possible, use only one exit statement
- Keep multiple exit statements together
Named Loops
- Loops can be named
- Example:
Search: -- Name the loop
for i in 1 .. 10 loop
for j in 1 .. 20 loop
...
exit when ... -- Exits inner loop
...
exit Search when ... -- Exits outer loop
end loop;
end loop Search;
If Statements
- No parens around condition
-
then
required
-
end if;
is required, even if body has only
a single statement
- keyword
elsif
- allows multiple conditions in single if
- avoids nesting
- Can have as many
elsif
statements as
desired
-
else
statement optional
- Two kinds of languages regarding brackets
- brackets optional for single line bodies: no elsif
- brackets always required: elsif
- Remember: if and for statements have
end if;
and
end for;
- Remember goal: concern for programmer
Case Statement - Multiway Selection
- Java version - what is the output?
i = 3;
switch (i){
case 4: System.out.println("****");
case 3: System.out.println("***");
case 2: System.out.println("**");
case 1: System.out.println("*");
default: ;
}
Ada version - what is the output?
i := 3;
case i is
when 4 => put_line("****");
when 3 => put_line("***");
when 2 => put_line("**");
when 1 => put_line("*");
when others => null;
end case;
Ada does not fall through to next case
when others
required if all values not accounted for
Ada uses null;
instead of solely ;
Case Statement - Multiple-Value Selection
- Multiple value selection in Ada and Java:
-- Ada:
case i is
when 4|3 => put_line("****");
when 2|1 => put_line("**");
when others => null;
end case;
// Java
switch (i){
case 4:
case 3: System.out.println("****"); break;
case 2:
case 1: System.out.println("**"); break;
default: ;
} // End of break
Some Java Code
if (b == 0)
if (a > 0)
res = MAX_INT;
else
res = a / b;
b++;
Under what conditions is i = j
executed?
if (a == b)
if (e == f)
g = h;
else
i = j;
What about this code:
// If signal is clear, increase speed
void increase_speed_if_safe (int speed, int signal)
{
if (signal == CLEAR && speed < MAX_SPEED);
increase_speed ();
}
How many times is the S.o.p
executed?
i = 0;
while (i <= 10);
System.out.println(i++);
Some test code
and a
prettified version
How to code these in Ada?
Example Program
- Let's get some practice by writing a simple program
- Let's sum a group of integers and calculate and print their average
- First we'll input the number of numbers and read that many numbers
- Can we format the average of the numbers
- What if the numbers are floating point?
- Let's make sure the number of numbers is non-negative
- Now let's read until eof