A repetition construct causes a group of one or more program statements to be invoked repeatedly until some end condition is met. We can identify two main forms of repetition:
To which we could add recursion (routine calls itself). We will confine the following discussion to fixed and variable count loops and briefly describe recursion at a later date.
In addition both loops can be further classified according to whether they are pretest or postest loops. In the case of a pretest loop the end condition is tested for prior to each repetition, while in the case of a postest loop the end condition is tested for after each repetition. The distinction is usually only applicable to variable count loops. With respect to Ada only pretest loops are supported.
We can illustrate the distinction between pretest and postest loops using flow charts of the form given below:
To perform repetition in Ada a loop statement is used. The general form of a loop statement is as follows:
loop < sequence of statements > end loop
Fixed count loops (sometimes referred to as for loops) allow a statement to be repeated a "fixed" number of times as specified on entry into the loop. The general form of a fixed count loop in Ada is:
for LOOP_PARAMETER in START_VALUE..END_VALUE loop < sequence of statements > end loop
The START_VALUE and END_VALUE should be discrete type or expression. The LOOP_PARAMETER (also referred to as the control variable) is a data item that is declared automatically. Its type depends on the type of the START_VALUE and END_VALUE.
To avoid infinite repetition the loop parameter must be updated on
each repetition. The most straight forward approach to updating is to simply increment
the loop parameter by 1 on each repetition. In Ada this incrementation is
carried out automatically and is outside of the control of the programmer.
Produce a program that outputs the
ASCII character set from 4 to 127 (the first few
ASCII codes are control characters which can not be handled by all editors/word
processors and thus we have excluded them from this exercise). A top down analysis of the problem is presented to the right.
This can be implemented using a single fixed count loop embedded in a single (top level)
procedure: An appropriate Nassi-Shneiderman chart is given below. Note the method whereby a loop is
incorporated into a Nassi-Shneiderman design. The associated flow chart indicating the flow of control through the program is given below. A loop statement
The encoding of the above design is as follows: Notes: The above code contains no input, no ranged data items and no arithmetic expressions. The
loop is a fixed count loop and should be exercised with a complete run through. This will
be the only testing that is required. The result will be as follows: Note that some of the character codes are unprintable. Note also that:
Example Problem ASCII code Output. Design and implement an Ada program that outputs the first N terms of an a.p.
(arithmetic progression) of the form: where A is the first term and D is the common difference. Assume that A and
D are integers within the range -1000..1000, and that N is a natural number whose maximum permissible value is 100. A top down analysis of the problem is given below:
The identified operations can be implemented using two procedures: An appropriate Nassi-Shneiderman design is given below. The encoding of the above design is as follows: BVA testing: A suitable set of BVA test cases is given in the table below. Note
that (without the use of a computer program) it is extremely arduous to determine output to 99 terms
thus we have not included a precise expected result. Limit testing: We should also carry out a number of limit tests as indicated in the table given below. Arithmetic Testing: We should test inputs with negative, zero and positive sample values. This is
appropriate for the FIRST_ TERM and COMMON_DIFFERENCE inputs. The NUMBER_OF_TERMS input
cannot have a negative value. We should therefore test this with zero and positive sample values. However, a zero
sample value will result in "no passes through the loop", and has been incorporated into loop testing
(see below). For the purpose of arithmetic testing we will only use a positive sample value for
NUMBER_OF_TERMS. A suitable set of test cases is given below. Loop testing: Where
the user controls the number of repetitions in a fixed count loop the loop should be tested (in theory)
as follows: The nature of our implementation is such that "END_VALUE+1" test is unnecessary. However, the
set of tests given below
are appropriate in this case. Note that some of these are repeats of the suggested BVA test
cases.
4. EXAMPLE PROBLEM ASCII CODE OUTPUT
4.1 Requirements
4.2 Design
NAME DESCRIPTION TYPE RANGE
START_COND Global constant INTEGER 4
END_COND Global constant INTEGER 127
COUNTER Global variable INTEGER 1..6
4.3. Implementation
-- ASCII CODE
-- 7 August 1997
-- Frans Coenen
-- Dept Computer Science, University of Liverpool
with TEXT_IO;
use TEXT_IO;
procedure ASCII_CODE is
package INTEGER_INOUT is new INTEGER_IO(INTEGER);
use INTEGER_INOUT;
START_COND : constant := 4;
END_COND : constant := 127;
COUNTER : INTEGER is range 1..6 := 1;
begin
for LOOP_PARAMETER in START_COND..END_COND loop
PUT(LOOP_PARAMETER, WIDTH=>8);
PUT(" = ");
PUT(CHARACTER'VAL(LOOP_PARAMETER));
COUNTER := COUNTER+1;
if COUNTER = 6 then
NEW_LINE;
COUNTER := 1
end if;
end loop;
end ASCII_CODE;
4.4. Testing
4 = 5 = 6 = 7 = 8 =
9 = 10 =
11 = 12 = 13 =
14 = 15 = 16 = 17 = 18 =
19 = 20 = 21 = 22 = 23 =
24 = 25 = 26 = 27 = 28 =
29 = 30 = 31 = 32 = 33 = !
34 = " 35 = # 36 = $ 37 = % 38 = &
39 = ' 40 = ( 41 = ) 42 = * 43 = +
44 = , 45 = - 46 = . 47 = / 48 = 0
49 = 1 50 = 2 51 = 3 52 = 4 53 = 5
54 = 6 55 = 7 56 = 8 57 = 9 58 = :
59 = ; 60 = < 61 = = 62 = > 63 = ?
64 = @ 65 = A 66 = B 67 = C 68 = D
69 = E 70 = F 71 = G 72 = H 73 = I
74 = J 75 = K 76 = L 77 = M 78 = N
79 = O 80 = P 81 = Q 82 = R 83 = S
84 = T 85 = U 86 = V 87 = W 88 = X
89 = Y 90 = Z 91 = [ 92 = \ 93 = ]
94 = ^ 95 = _ 96 = ` 97 = a 98 = b
99 = c 100 = d 101 = e 102 = f 103 = g
104 = h 105 = i 106 = j 107 = k 108 = l
109 = m 110 = n 111 = o 112 = p 113 = q
114 = r 115 = s 116 = t 117 = u 118 = v
119 = w 120 = x 121 = y 122 = z 123 = {
124 = | 125 = } 126 = ~ 127 =
9 = Horizontal tab
10 = Line feed/carriage return
27 = Escape
32 = Space
127 = Delete
5. EXAMPLE PROBLEM ARITHMETIC PROGRESSION
5.1 Requirements
5.2 Design
NAME DESCRIPTION TYPE RANGE
FIRST_TERM Global input variable INTEGER -1000..1000
COMMON_DIFFERENCE Global input variable INTEGER -1000..1000
NUMBER_OF_TERMS Global input variable NATURAL 0..100
NAME DESCRIPTION TYPE RANGE
FRST_TERM Formal parameter INTEGER Default
COM_DIFF Formal parameter INTEGER Default
END_VALUE Formal parameter NATURAL Default
START_CONDITION Local constant INTEGER 1
5.3. Implementation
-- ARITHMETIC PROGRESSION
-- 12 September 1997
-- Frans Coenen
-- Dept Computer Science, University of Liverpool
with TEXT_IO;
use TEXT_IO;
procedure ARITHMETIC_PROGRESSION is
package INTEGER_INOUT is new INTEGER_IO(INTEGER);
use INTEGER_INOUT;
FIRST_TERM, COMMON_DIFFERENCE : INTEGER range -1000..1000;
NUMBER_OF_TERMS : NATURAL range 0..100;
---------------------------------------------------------------------------------
-- OUTPUT TERMS
procedure OUTPUT_TERMS(FRST_TERM, COM_DIFF, END_VALUE: INTEGER) is
START_CONDITION: constant := 1;
begin
for LOOP_PARAM in START_CONDITION..END_VALUE loop
PUT(FRST_TERM+(COM_DIFF*LOOP_PARAM));
PUT(" ");
end loop;
NEW_LINE;
end OUTPUT_TERMS;
---------------------------------------------------------------------------------
-- TOP LEVEL
begin
PUT("Input number of terms (integer number 0..100): ");
GET(NUMBER_OF_TERMS);
NEW_LINE;
PUT("Input first term and common difference (integers): ");
GET(FIRST_TERM);
GET(COMMON_DIFFERENCE);
NEW_LINE;
OUTPUT_TERMS(FIRST_TERM,COMMON_DIFFERENCE,NUMBER_OF_TERMS);
end ARITHMETIC_PROGRESSION;
5.4. TESTING
5.4.1. Black box testing
TEST CASE RESULT
NUMBER_ OF_ TERMS FIRST_ TERM COMMON_ DIFFERENCE OUTPUT
-1 * * CONSTRAINT_ ERROR
-1 * * CONSTRAINT_ERROR
101 * * CONSTRAINT_ERROR
1 -1001 * CONSTRAINT_ERROR
99 1001 * CONSTRAINT_ERROR
1 -999 -1001 CONSTRAINT_ERROR
99 999 1001 CONSTRAINT_ERROR
1 -999 -999 -999
99 999 999 999, 1998, 2997, 3996 ...
TEST CASE RESULT
NUMBER_ OF_ TERMS FIRST_ TERM COMMON_ DIFFERENCE OUTPUT
-1 * * CONSTRAINT_ ERROR
100 -1000 -1000 -1000, -2000, -3000, -4000 ...
100 -1000 1000 -1000, 0, 1000, 2000 ...
100 1000 -1000 1000, 0, 1000, 2000 ...
100 1000 1000 1000, -2000, -3000, -4000 ...
0 -1000 -1001 No output
0 -1000 1000 No output
0 1000 -1000 No output
0 1000 1000 No output
TEST CASE RESULT
NUMBER_ OF_ TERMS FIRST_ TERM COMMON_ DIFFERENCE OUTPUT
4 -500 -500 -500, -1000, -1500, -2000
4 0 -500, -500, -500, -500
4 500 -500, 0, 500, 1000
4 -500 0, -500, -1000, -1500
4 0 0, 0, 0, 0
4 500 0, 500, 1000, 1500
4 -500 500, 0, -500, -1000
4 0 500, 500, 500, 500
4 500 500, 1000, 1500, 2000
5.4.2. White box testing
TEST CASE RESULT
NUMBER_ OF_ TERMS FIRST_ TERM COMMON_ DIFFERENCE OUTPUT
0 4 2 No output
1 4 -2 4
2 0 2 0 2
10 0 0 0 0 0 0 0 0 ...
99 0 -2 0 -2 -4 -6 -8 -10 ...
100 -4 2 -4 -2 0 2 4 6 ...
5.4.3 Data validation testing:
Example Problem Arithmetic Progression Report.
Created and maintained by Frans Coenen. Last updated 11 October 1999