Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration.

Similar presentations


Presentation on theme: "Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration."— Presentation transcript:

1 Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration

2 LOOP - Repetition of steps in a program - Loop body contains the steps to be repeated

3 Example 1 Ada.Text_IO.Put ( Item => “ Hello there. “ ); - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - FOR Count IN 1..5 LOOP Ada.Text_IO.Put ( Item => “ Hello there. “ ); Ada.Text_IO.Put ( Item => “ Hello there. “ ); END LOOP;

4 Example 2 How could we write to print numbers as follow: 123..100

5 Here is what we learned so far I:= 1; Ada.Integer_Text_IO ( Item => I ); Ada.Text_IO.New_Line; I := I +1; Ada.Integer_Text_IO ( Item => I ); Ada.Text_IO.New_Line; I := I +1; Ada.Integer_Text_IO ( Item => I ); Ada.Text_IO.New_Line;..

6 Using FOR statement FOR I In 1.. 100 LOOP Ada.Integer_Text_IO ( Item => I ); Ada.Integer_Text_IO ( Item => I ); Ada.Text_IO.New_Line; Ada.Text_IO.New_Line; END LOOP;

7 FOR statement FOR counter IN lowbound.. Highbound LOOP statement sequence statement sequence END LOOP FOR counter IN REVERSE lowbound.. Highbound LOOP statement sequence statement sequence END LOOP

8 To list Number, Square and Cube Starting from 1 WITH Ada.Text_IO; WITH Ada.Integer_Text_IO; Procedure Square_Cube Is --------------------------- -- to display square and cube of a number from 1 to n -- Input max (say here 10 ) -- Output number square cube -- As follows: -- Number Square Cube -- 1 1 1 -- 2 4 8 -- 3 9 27 --... -- 10 100 1000 ---------------------------

9 Cont. 1 of square_cube max, Square, Cube : natural; Begin -- Ada.Text_IO.Put ( Item => " Give maximum value to find square and cube "); Ada.Text_IO.New_Line; Ada.Integer_Text_IO.Get ( Item => max ); Ada.Text_IO.Put ( Item => " Num Square Cube "); Ada.Text_IO.New_Line; Ada.Text_IO.Put ( Item => " --- ------ ------ "); Ada.Text_IO.New_Line;

10 Cont. 2 of square_cube FOR Num IN 1..max LOOP Square := Num * Num; Cube := Num * Num * Num; Ada.Integer_Text_IO.Put ( Item => Num, width=>10); Ada.Integer_Text_IO.Put ( Item => Square, width=>10); Ada.Integer_Text_IO.Put ( Item => Cube, width=>10); Ada.Text_IO.New_Line; END LOOP; END Square_Cube;

11 Find the sum of all integers from 1 to N WITH Ada.Text_IO; WITH Ada.Integer_Text_IO; Procedure Sum_Integer Is --------------------------- -- to display sum of all integers from 1 to N -- Input N -- Output the sum of 1 to N --------------------------- N : Positive; ---- input Sum : Natural; ---- output

12 Cont. 1 of finding sum Begin -- Ada.Text_IO.Put ( Item => " Enter the last integer in the sum: ” "); Ada.Integer_Text_IO.Get ( Item => N ); ----- to find the sum from 1 to N Sum := 0; ------ initialize sum to 0 FOR I IN 1..N LOOP Sum := Sum + I; ----- add next integer to sum END LOOP; Ada.Text_IO.Put ( Item => " The sum of the integers from 1 to "); Ada.Integer_Text_IO.Put ( Item => N, Width =>1); Ada.Text_IO.Put ( Item => " is "); Ada.Integer_Text_IO.Put ( Item => Sum, Width =>1) Ada.Text_IO.New_Line; END Sum_Integers ;

13 Nested Loop Nested Loop consists of an outer loop with one or more inner loops. with one or more inner loops. Each time the outer loop is repeated, the inner loops are reentered. the inner loops are reentered. Their loop control parameters are reevaluated and are reevaluated and required iterations are performed. required iterations are performed.

14 Example-1 For I in 1..5 Loop For J in 1..3 Loop For J in 1..3 Loop Ada.Integer_Text_IO.Put( Item=>I, width=>1); Ada.Integer_Text_IO.Put( Item=>I, width=>1); Ada.Integer_Text_IO.Put( Item=>J, width=>1); Ada.Integer_Text_IO.Put( Item=>J, width=>1); Ada.Text_IO.Put( Item=>” “); Ada.Text_IO.Put( Item=>” “); End Loop; - - - - inner loop finished End Loop; - - - - inner loop finished Ada.Text_IO.New_Line; Ada.Text_IO.New_Line; End Loop; - - - - outer loop finished What will be displayed?

15 Output for Example - 1 11 12 13 21 22 23 31 32 33 41 42 43 51 52 53

16 Example -2 Sum := 0; For I in 1..5 Loop For J in 1..3 Loop For J in 1..3 Loop Sum := I + J; Sum := I + J; Ada. Text_IO.Put( Item=> “I + J = “); Ada. Text_IO.Put( Item=> “I + J = “); Ada.Integer_Text_IO.Put( Item=>Sum, width=>1); Ada.Integer_Text_IO.Put( Item=>Sum, width=>1); Ada.Text_IO.Put( Item=>”, “); Ada.Text_IO.Put( Item=>”, “); End Loop; - - - - inner loop finished End Loop; - - - - inner loop finished Ada.Text_IO.New_Line; Ada.Text_IO.New_Line; End Loop; - - - - outer loop finished What will be displayed?

17 Output for example-2 I + J = 2, I + J = 3, I + J = 4, I + J = 3, I + J = 4, I + J = 5, I + J = 4, I + J = 5, I + J = 6, I + J = 5, I + J = 6, I + J = 7, I + J = 6, I + J = 7, I + J = 8,

18 Example - 3 For I in 1..5 Loop Sum := 0; Sum := 0; For J in 1..3 Loop For J in 1..3 Loop Sum := I + J; Sum := I + J; Ada. Text_IO.Put( Item=> “I + J = “); Ada. Text_IO.Put( Item=> “I + J = “); Ada.Integer_Text_IO.Put( Item=>Sum, width=>1); Ada.Integer_Text_IO.Put( Item=>Sum, width=>1); Ada.Text_IO.Put( Item=>” “); Ada.Text_IO.Put( Item=>” “); End Loop; - - - - inner loop finished End Loop; - - - - inner loop finished Ada.Text_IO.New_Line; Ada.Text_IO.New_Line; End Loop; - - - - outer loop finished What will be displayed?

19 Output for Exmple -3 I + J = 2, I + J = 3, I + J = 4, I + J = 3, I + J = 4, I + J = 5, I + J = 4, I + J = 5, I + J = 6, I + J = 5, I + J = 6, I + J = 7, I + J = 6, I + J = 7, I + J = 8,

20 Example - 4 Sum := 0; For I in 1..5 Loop For J in 1..3 Loop For J in 1..3 Loop Sum :=Sum + I + J; Sum :=Sum + I + J; Ada. Text_IO.Put( Item=> “I + J = “); Ada. Text_IO.Put( Item=> “I + J = “); Ada.Integer_Text_IO.Put( Item=>Sum, width=>1); Ada.Integer_Text_IO.Put( Item=>Sum, width=>1); Ada.Text_IO.Put( Item=>” “); Ada.Text_IO.Put( Item=>” “); End Loop; - - - - inner loop finished End Loop; - - - - inner loop finished Ada.Text_IO.New_Line; Ada.Text_IO.New_Line; End Loop; - - - - outer loop finished What will be displayed?

21 Output for Example-4 I + J = 2, I + J = 3, I + J = 4, I + J = 7, I + J = 8, I + J = 9, I + J = 13, I + J = 14, I + J = 15, I + J = 20, I + J = 21, I + J = 22, I + J = 28, I + J = 29, I + J = 30,

22 Example-5 For I in 1..5 Loop Sum := 0; Sum := 0; For J in 1..3 Loop For J in 1..3 Loop Sum :=Sum + I + J; Sum :=Sum + I + J; Ada. Text_IO.Put( Item=> “I + J = “); Ada. Text_IO.Put( Item=> “I + J = “); Ada.Integer_Text_IO.Put( Item=>Sum, width=>1); Ada.Integer_Text_IO.Put( Item=>Sum, width=>1); Ada.Text_IO.Put( Item=>” “); Ada.Text_IO.Put( Item=>” “); End Loop; - - - - inner loop finished End Loop; - - - - inner loop finished Ada.Text_IO.New_Line; Ada.Text_IO.New_Line; End Loop; - - - - outer loop finished What will be displayed?

23 Output for Example - 5 I + J = 2, I + J = 5, I + J = 9, I + J = 3, I + J = 7, I + J = 12, I + J = 4, I + J = 9, I + J = 15, I + J = 5, I + J = 11, I + J = 17, I + J = 6, I + J = 13, I + J = 19,

24 Example - 6 For I in 1..5 Loop For J in 1..3 Loop For J in 1..3 Loop Sum := 0; Sum := 0; Sum :=Sum + I + J; Sum :=Sum + I + J; Ada. Text_IO.Put( Item=> “I + J = “); Ada. Text_IO.Put( Item=> “I + J = “); Ada.Integer_Text_IO.Put( Item=>Sum, width=>1); Ada.Integer_Text_IO.Put( Item=>Sum, width=>1); Ada.Text_IO.Put( Item=>” “); Ada.Text_IO.Put( Item=>” “); End Loop; - - - - inner loop finished End Loop; - - - - inner loop finished Ada.Text_IO.New_Line; Ada.Text_IO.New_Line; End Loop; - - - - outer loop finished What will be displayed?

25 Output for Example - 6 I + J = 2, I + J = 3, I + J = 4, I + J = 3, I + J = 4, I + J = 5, I + J = 4, I + J = 5, I + J = 6, I + J = 5, I + J = 6, I + J = 7, I + J = 6, I + J = 7, I + J = 8,

26 Example - - - Outer- Inner Loop BEGIN -- Nested_Loops Ada.Text_IO.Put (Item => " OuterCounter InnerCounter"); Ada.Text_IO.New_Line;

27 Cont. Outer - Inner Loop FOR OuterCounter IN 1.. 3 LOOP Ada.Text_IO.Put(Item => "OUTER"); Ada.Integer_Text_IO.Put (Item => OuterCounter, Width => 10); Ada.Text_IO.New_Line; FOR InnerCounter IN 1.. OuterCounter LOOP Ada.Text_IO.Put(Item => " INNER"); Ada.Integer_Text_IO.Put (Item => InnerCounter, Width => 22); Ada.Text_IO.New_Line; END LOOP; END Nested_Loops;

28 Example - - - Triangle NumLines: CONSTANT Integer := 5; Blank : CONSTANT Character := ' '; Star : CONSTANT Character := '*';

29 Cont... Triangle BEGIN -- Triangle FOR Row IN 1.. NumLines LOOP -- draw each row FOR LeadBlanks IN REVERSE 1.. NumLines - Row LOOP Ada.Text_IO.Put(Item => Blank); -- leading blanks END LOOP; FOR CountStars IN 1.. (2*Row) - 1 LOOP Ada.Text_IO.Put(Item => Star); -- display asterisks END LOOP; Ada.Text_IO.New_Line; -- terminate row END LOOP; END Triangle;

30 Subtype of Enumeration Types Subtype of programmer’s defined types can be defined just as easily as can be defined just as easily as subtypes of predefined types. subtypes of predefined types.

31 Example Type Months IS ( Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec); Jul, Aug, Sep, Oct, Nov, Dec); Subtype Spring IS Month Range Mar.. May; Subtype Summer IS Month Range Jun.. Aug; Subtype Autumn IS Month Range Sep.. Nov;

32 Note Ada requires that the values of a subtype be specified in the form of a range and therefore contiguous, that is, adjacent in the base type definition. that is, adjacent in the base type definition. Can we define as follow ? Subtype Winter IS Months Range Dec.. Feb;

33 Example WorkDay WITH Ada.Text_IO; PROCEDURE Work_Days IS -------------------------------------------------------------- --| Demonstrates the use of enumeration subtypes: --| prompts user for a day of the week and determines whether --| the following day is a weekday or weekend day. -------------------------------------------------------------- TYPE Days IS (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); SUBTYPE WeekDays IS Days RANGE Monday.. Friday; SUBTYPE WeekEnd IS Days RANGE Saturday.. Sunday; PACKAGE Day_IO IS NEW Ada.Text_IO.Enumeration_IO (Enum => Days);

34 Cont.. Example Today : Days; -- input - day of the week Tomorrow : Days; -- output - next day BEGIN -- Work_Days -- prompt user to enter a day name Ada.Text_IO.Put (Item => "Enter a day of the week > "); Day_IO.Get (Item => Today); -- find tomorrow IF Today = Days'Last THEN Tomorrow := Days'First; ELSE Tomorrow := Days'Succ(Today); END IF;

35 Cont.. Example Ada.Text_IO.Put (Item => "Tomorrow is "); Day_IO.Put (Item => Tomorrow); Ada.Text_IO.New_Line; -- Is Tomorrow a week day or a weekend day? IF Tomorrow IN Weekdays THEN Ada.Text_IO.Put (Item => "Another day, another dollar..."); Ada.Text_IO.New_Line; ELSE Ada.Text_IO.Put (Item => "We've worked hard, let's play hard!"); Ada.Text_IO.New_Line; END IF; Ada.Text_IO.Put (Item => "Have a good day tomorrow."); Ada.Text_IO.New_Line; END Work_Days;

36 Addition Table WITH Ada.Text_IO; WITH Ada.Integer_Text_IO; PROCEDURE Addition_Table IS -------------------------------------------------------------- --| Displays an addition table. --| -------------------------------------------------------------- MaxDigit : CONSTANT Natural := 9; SUBTYPE SmallNatural IS Natural RANGE 0.. MaxDigit;

37 Cont.. Addition Table BEGIN -- Addition_Table -- Display the table heading. Ada.Text_IO.Put(Item => "+"); FOR Right IN SmallNatural LOOP -- Display heading Ada.Integer_Text_IO.Put(Item => Right, Width => 3); END LOOP; Ada.Text_IO.New_Line; -- Terminate heading

38 Cont.. Addition Table -- Display the table body. FOR Left IN SmallNatural LOOP -- Display each row of the table Ada.Integer_Text_IO.Put(Item => Left, Width => 1); FOR Right IN SmallNatural LOOP Ada.Integer_Text_IO.Put (Item => Left + Right, Width => 3); END LOOP; Ada.Text_IO.New_Line; -- Terminate table row END LOOP; END Addition_Table;

39 Format for FOR statement FOR counter IN type-name LOOP statement sequence statement sequence END LOOP; FOR counter IN REVERSE type-name LOOP statement sequence statement sequence END LOOP;

40 Limitation of the FOR statement Counter is always either b incremented ( by taking the successor) b decremented ( by taking the predecessor) Only to loop through all the values of a given range no way to loop alternate counter such as by 2

41 Using an External File for Input Data b Create a data file in disk using an editor (vi) b Declare the name of the file to be used in program b Associate the name of the file in the program with the name of the file in disk. b Write Input Operation to read data from file

42 1. Create a data file in disk using an editor (vi) Example >vi scores.dat 557221007682

43 2. Declare the name of the file to be used in program Declaration TestScore : Ada.Text_IO.File_Type; TestScore : Ada.Text_IO.File_Type;

44 3. Associate the name of the file in the program with the name of the file in disk. Ada.Text_IO.Open ( File => TestScore, Mode => Ada.Text_IO.In_File, ( File => TestScore, Mode => Ada.Text_IO.In_File, Name => “score.dat” ); Name => “score.dat” );

45 4. Write Input Operation to read data from file Ada.Integer_Text_IO.Get (File => TestScore, Item => CurrentValue);

46 WITH Ada.Text_IO; WITH Ada.Integer_Text_IO; WITH Min_Max; PROCEDURE Min_Max_Average_File IS ------------------------------------------------------------------------ --| Finds and displays the minimum, maximum, and average --| of a list of data items; the data items are read from a file. --| ------------------------------------------------------------------------ NumValues: Positive; -- input - the number of items to be averaged CurrentValue: Integer; -- input - the next data item to be added Smallest: Integer; -- output - minimum of the data values Largest: Integer; -- output - maximum of the data values Average: Integer; -- output - average of the data values Sum: Integer; -- program variable - sum being accumulated TestScores: Ada.Text_IO.File_Type; -- program variable - names the input file -- Step 2

47 BEGIN -- Min_Max_Average_File -- Open the file and associate it with the file variable name -- Step 3 Ada.Text_IO.Open (File => TestScores, Mode => Ada.Text_IO.In_File, Name => "scores.dat"); -- Read from the file the number of items to be averaged -- Step 4 Ada.Integer_Text_IO.Get(File => TestScores, Item => NumValues); Ada.Text_IO.Put("The number of scores to be averaged is "); Ada.Integer_Text_IO.Put(Item => NumValues, Width => 1); Ada.Text_IO.New_Line; -- Initialize program variables Smallest := Integer'Last; Largest := Integer'First; Sum := 0;

48 -- Initialize program variables Smallest := Integer'Last; Largest := Integer'First; Sum := 0; -- Read each data item, log to the screen, add it to Sum, -- and check if it is a new minimum or maximum FOR Count IN 1.. NumValues LOOP Ada.Integer_Text_IO.Get(File => TestScores, Item => CurrentValue); Ada.Text_IO.Put("Score number "); Ada.Integer_Text_IO.Put(Item => Count, Width => 1); Ada.Text_IO.Put(" is "); Ada.Integer_Text_IO.Put(Item => CurrentValue, Width => 1); Ada.Text_IO.New_Line; Sum := Sum + CurrentValue; Smallest := Min_Max.Minimum(Value1 => Smallest, Value2 => CurrentValue); Largest := Min_Max.Maximum(Value1 => Largest, Value2 => CurrentValue); END LOOP;

49 -- compute the average; since Sum and NumValues are integers, -- the fractional part of the average is discarded Average := Sum / NumValues; -- display the results Ada.Text_IO.Put(Item => "The Smallest is "); Ada.Integer_Text_IO.Put(Item => Smallest, Width => 1); Ada.Text_IO.New_Line; Ada.Text_IO.Put(Item => "The Largest is "); Ada.Integer_Text_IO.Put(Item => Largest, Width => 1); Ada.Text_IO.New_Line; Ada.Text_IO.Put(Item => "The Average is "); Ada.Integer_Text_IO.Put(Item => Average, Width => 1); Ada.Text_IO.New_Line; END Min_Max_Average_File;

50 Payroll Example WITH Ada.Text_IO; WITH Ada.Integer_Text_IO; WITH Ada.Float_Text_IO; PROCEDURE Multi_Payroll IS ---------------------------------------------------------------- --| Computes and displays gross pay and net pay for a number --| of employees, given each employee's hourly rate and --| hours worked. --|----------------------------------------------------------------

51 Cont.. Payroll SUBTYPE NonNegFloat IS Float RANGE 0.0.. Float'Last; TaxBracket : CONSTANT NonNegFloat := 100.00; -- maximum salary for no tax TaxRate : CONSTANT NonNegFloat := 0.15; -- tax rate NumEmp : Positive; -- inputs - number of employees Hours : NonNegFloat; -- hours worked, hourly rate HourlyRate: NonNegFloat; GrossPay: NonNegFloat; -- outputs - gross pay, net pay Tax: NonNegFloat; NetPay: NonNegFloat;

52 Cont.. Payroll BEGIN -- Multi_Payroll Ada.Text_IO.Put ("Please enter number of employees > "); Ada.Integer_Text_IO.Get (Item => NumEmp); FOR CountEmp IN 1.. NumEmp LOOP -- Enter Hours and HourlyRate Ada.Text_IO.Put (Item => "Employee number "); Ada.Integer_Text_IO.Put (Item => CountEmp, Width => 1); Ada.Text_IO. New_Line; Ada.Text_IO.Put (Item => " Hours worked > "); Ada.Float_Text_IO.Get (Item => Hours); Ada.Text_IO.Put (Item => " Hourly rate $"); Ada.Float_Text_IO.Get (Item => HourlyRate); -- Compute gross salary GrossPay := Hours * HourlyRate;

53 Cont.. Payroll -- Compute net salary IF GrossPay > TaxBracket THEN Tax := TaxRate * (GrossPay - TaxBracket); NetPay := GrossPay - Tax; -- Deduct a tax amount ELSE NetPay := GrossPay; -- Deduct no tax END IF;

54 Cont.. Payroll example -- Display Results Ada.Text_IO.Put (Item => " Gross pay is $"); Ada.Float_Text_IO.Put (Item => GrossPay, Fore => 1, Aft => 2, Exp => 0); Ada.Text_IO.New_Line; Ada.Text_IO.Put (Item => " Tax Deduction is $"); Ada.Float_Text_IO.Put (Item => Tax, Fore => 1, Aft => 2, Exp => 0); Ada.Text_IO.New_Line; Ada.Text_IO.Put (Item => " Net pay is $"); Ada.Float_Text_IO.Put (Item => NetPay, Fore => 1, Aft => 2, Exp => 0); Ada.Text_IO.New_Line; END LOOP; END Multi_Payroll;

55 Overloading b Ada allows two or more different operations to have the same name with enough difference in their parameter profiles b The same name b Different parameter profiles b Compiler can distinguish them

56 Advantage b Operations with similar behavior or functionality can be given similar names b Easier to write and read

57 How When compiler reaches a procedure or function call b examine the parameter profile b select the appropriate procedure

58 Example for overloading PACKAGE Useful_Functions IS -------------------------------------------------------------- FUNCTION Minimum (Value1, Value2: Integer) RETURN Integer; FUNCTION Minimum (Value1, Value2: Float) RETURN Float; -- Pre: Value1 and Value2 have been assigned values -- Post: Returns the smaller of the two input values FUNCTION Maximum (Value1, Value2: Integer) RETURN Integer; FUNCTION Maximum (Value1, Value2: Float) RETURN Float; -- Pre: Value1 and Value2 have been assigned values -- Post: Returns the larger of the two input values

59 Use Overloading Wisely b Give meaningful names b Give same name with similar functionality b Do not abuse by using it too much or by using it to name functions and procedures that do not have similar behavior

60 Cont. example FUNCTION Sum (N: Positive) RETURN Positive; -- Pre: N has been assigned a value -- Post: Returns the sum of integers from 1 to N FUNCTION Factorial (N: Positive) RETURN Positive; -- Pre: N has been assigned a value -- Post: Returns the product of integers from 1 to N END Useful_Functions;

61 Example - body of Useful_Functions FUNCTION Minimum (Value1, Value2: Integer) RETURN Integer IS Result: Integer; BEGIN -- Minimum IF Value1 < Value2 THEN Result := Value1; ELSE Result := Value2; END IF; RETURN Result; END Minimum; -- minimum of two Float values

62 Cont. example FUNCTION Minimum (Value1, Value2: Float) RETURN Float IS Result: Float; BEGIN -- Minimum IF Value1 < Value2 THEN Result := Value1; ELSE Result := Value2; END IF; RETURN Result; END Minimum;

63 Example using overlaoding WITH Ada.Text_IO; WITH Ada.Integer_Text_IO; WITH Ada.Float_Text_IO; WITH Useful_Functions; PROCEDURE Max_Int_Flt IS -------------------------------------------------------------- --| -------------------------------------------------------------- Int1 : Integer; -- inputs Int2 : Integer; LargerInt : Integer; -- output Flt1 : Float; -- inputs Flt2 : Float; LargerFlt : Float; -- output

64 BEGIN -- Max_Int_Flt Ada.Text_IO.Put (Item => "Please enter first integer value > "); Ada.Integer_Text_IO.Get (Item => Int1); Ada.Text_IO.Put (Item => "Please enter second integer value > "); Ada.Integer_Text_IO.Get (Item => Int2); LargerInt := Useful_Functions.Maximum(Value1=>Int1, Value2=>Int2); Ada.Text_IO.Put (Item => "The larger integer is "); Ada.Integer_Text_IO.Put (Item => LargerInt, Width => 1); Ada.Text_IO.New_Line; Ada.Text_IO.Put (Item => "Please enter first float value > "); Ada.Float_Text_IO.Get (Item => Flt1); Ada.Text_IO.Put (Item => "Please enter second float value > "); Ada.Float_Text_IO.Get (Item => Flt2); LargerFlt := Useful_Functions.Maximum(Value1=>Flt1, Value2=>Flt2); Ada.Text_IO.Put (Item => "The larger float is "); Ada.Float_Text_IO.Put (Item => LargerFlt, Fore => 1, Aft => 2, Exp => 0); Ada.Text_IO.New_Line; END Max_Int_Flt;

65 Exceptional Handling Ada allows the programmer to catch the exception before it goes to the Ada run- time system b out of range b beginning with a nonnumeric character b too large number

66 Error Messages and Exception b Constraint_Error ( out of range) b Ada.Text_IO.Data_Error ( bad data) EXCEPTION WHEN Constraint_Error => Ada.Text_IO.Put (Item => "The input value or result is out of range."); Ada.Text_IO.New_Line; WHEN Ada.Text_IO.Data_Error => Ada.Text_IO.Put (Item => "The input value is not well formed."); Ada.Text_IO.New_Line;


Download ppt "Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration."

Similar presentations


Ads by Google