Previous topic |
Ada Home Page |
Index
Example: while loop
--------------------------------------------------------
-- How long to be a millionaire?
-- adapted from Skansholm, section 2.4.8, page 59
--------------------------------------------------------
with TEXT_IO; use TEXT_IO; -- specify packages
procedure rich is
-- declare integer and float I/O libraries
package int_io is new TEXT_IO.INTEGER_IO( INTEGER );
package float_io is new TEXT_IO.FLOAT_IO( FLOAT );
use int_io, float_io;
-- constants and variables required
number_of_days : integer := 1;
days_wage : float := 0.01;
total_earnings : float := 0.01;
desired_earnings : constant float := 1000000.0;
begin -- rich
-- How many days needed, doubling income each day?
while total_earnings < desired_earnings loop
number_of_days := number_of_days + 1;
days_wage := days_wage * 2.0;
total_earnings := total_earnings + days_wage;
end loop;
-- tell us the good news!
put ("You will be a millionaire in ");
put (number_of_days, width=>1);
put_line ("days.");
end rich;
Previous topic |
Ada Home Page |
Index
c-lokan@adfa.oz.au / 12 Feb 96