Tuesday 2/26/02 Week 7

Chapter 6

Student Notes

 

Objectives

Review of FOR loop

Using an external file for input data

Nested loops

More Subtypes

the IN operator

Debugging tips

Operator Overloading

Intro to Exception Handling

Return Midterms

 

1.  Review of the FOR loop

·        The FOR loop is called a                            because the exact number of loop repetitions can be specified

·        FOR Count in 1..5 LOOP

    Ada.Text_IO.Put(Item => “Hello There”);

    Ada.Text_IO.New_Line;

  END LOOP;

·        Count is called the                       because its value controls the loop repetitions

·        Unlike other variables, a FOR loop counter is not declared. It is automatically of the type Integer

·        A loop counter may also be referenced

·         FOR Num IN 1..MaxNum LOOP

         NumSquared := Num**2;

         Ada.Integer_Text_IO.Put(Item => Num, Width => 10);

         Ada.Integer_Text_IO.Put(Item => NumSquared, Width => 10);

         Ada.Text_IO.New_Line;

      END LOOP;

·         Until now we have only used 1..MaxRange for the range of the loop, but note that we could also use a

·         SUBTYPE SmallInt IS Integer RANGE -50..50;

·         FOR Counter IN SmallInt LOOP

         Ada.Integer_Text_IO.Put(Item => Counter);

         Ada.Text_IO.New_Line;

      END LOOP;

 

2. REVERSE in FOR loops

·         

·        Ex from Program 6.2 page 203

·        FOR Num IN REVERSE MinNum..MaxNum LOOP

     NumSquared := Num**2;

     Ada.Integer_Text_IO.Put(Item => Num, Width => 10);

     Ada.Integer_Text_IO.Put(Item => NumSquared, Width => 10);

     Ada.Text_IO.New_Line;

   END LOOP;

·        Why use REVERSE and not just say

         FOR Num IN MaxNum..MinNum LOOP

·        In Ada, the FOR loop must run from

·        IF                                                                                                         , the statements inside the FOR loop will not be executed.

·        Note that this does not cause a compilation error, but there will be a logical error when your program does not run as you expected

 

 

 

3.  Limitations of the FOR loop

 

 

 

 

 

 

4.  Using an external file for input data

·        Input and output works very similarly with files as it does with screen input ant output

·        Ex. Program 6.5 page 215

·         

·         

·        To declare a file variable: TestScores : Ada.Text_IO.File_Type;

·        This file variable must be associated with an actual file existing in the same directory as the main program.

·        We do this when we open the file for reading

·        Ada.Text_IO.Open (File => TestScores, Mode => Ada.Text_IO.In_File,

                                          Name => “scores.dat”);

·         

·         

·         

·        The first number is 8 because that is the number of scores in this file

·         

·         

·         

·         

·         

·        Note that the file extension “.dat” is not important.

 

 

 

 

5.  Nested Loops

·        It is possible to put one loop inside another and it is often useful to do so

·        Program 6.7 page 221

·        Every statement inside the outermost loop is executed before the outermost loop loops again

·        This includes a loop inside another loop

·        Program 6.8 page 222

 

 

 

 

6.  More on Subtypes

·        It is possible to create a subtype of an enumeration type.

·        TYPE Months IS (January, February, March, April, May, June, July, August, September, October, November, December);

·        We can now define the following three seasons:

·        SUBTYPE Spring IS Months RANGE March..May;

·        SUBTYPE Summer IS Months RANGE June..August;

·        SUBTYPE Fall IS Months RANGE September..November;

·        What about Winter?

 

 

 

 

 

7.  Type Membership: the Operator IN

·        IN is used to decide

·         

·        SUBTYPE Weekdays IS Days RANGE Monday..Friday;

·        SUBTYPE Weekend IS Days RANGE Saturday..Sunday;

·        IF Today IN Weekdays THEN

     Ada.Text_IO.Put(Item => “Go to school today”);

   ELSE

     Ada.Text_IO.Put(Item => “Stay home and watch cartoons!”);

   END IF;

·        We also use IN in the FOR loop

 

8.  Debugging tips

·        Regression testing: once the code is through the compiler, you will probably run into logic errors. This means that the program runs, but incorrectly. Once you fix this error, go back and run all of your tests again. This makes sure that you don’t create another logic error in fixing the first.

·        Diagnostic output: put statements out to the screen at key points in the code to verify location in the code (handy if you are have trouble discovering what is causing a Constraint_Error, etc) or the current value of a variable.

 

9.  Operator Overloading

·        Extending the Min_Max Package, prog 6.13 page233

·        Notice that there are two functions for Minimum and two functions for Maximum

·         

·         

·         

·        They have different formal parameters and different return values. Therefore they are totally independent and different functions which happen to have the same name

·         

·         

·         

·         

·        We’ve been using overloaded operations all ready.

·        Consider the “+” operation. This works is we are adding two floats together OR two integers.

·         

·         

·         

·        Look at programs 6.15 and 6.16 for more overloading examples

 

10.  Exception handling

·        Ex. Prog 6.16

·         

·         

·         

·         

·        Instead of just letting the program crash, it would be nice to have a more graceful way to handle these errors

·         

·         

·        Ex prog 6.17 – Sum_and_Factorial with exception handling

·        The last section before END Robust_Sum_Fact is the

·         

·        EXCEPTION

  WHEN Constraint_Error =>

     Ada.Text_IO.Put(Item => “The input value 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”);

END Robust_Sum_Fact;

·         More on this is chapter 7