Previous topic |
Ada Home Page |
Index
Loop example
Accumulating a product or sum
A common requirement of a loop is to accumulate a sum or product, eg
tot := 0;
PUT("Enter j (-1 to exit): "); GET(j); SKIP_LINE;
while (j /= -1) loop
tot := tot + j;
PUT("Enter j (-1 to exit): "); GET(j); SKIP_LINE;
end loop;
PUT("Total is "); PUT(tot); NEW_LINE;
Important components are:
- initialise variable with sum or product outside loop
- update variable appropriately inside loop
Previous topic |
Ada Home Page |
Index
c-lokan@adfa.oz.au / 14 Feb 96