Previous | Next | Table of Contents | Index | Program List | Copyright

Chapter Review

This chapter introduced you to an important control structure, the IF statement, for building decision steps into programs. IF statements are of three types: single alternative, two-alternative, and multiple-alternative. IF statements provide a way to build decision making into a program. You also learned how to write simple user-defined functions and how to structure a package you are writing. A package consists of a specification file and a body file. The specification gives a "contract with the user," telling both the reader and the compiler what to expect in a package. The body then provides all the things promised by the specification.

New Ada Constructs in Chapter 4

The new Ada constructs introduced in this chapter are described in Table 4.7.

Table 4.7
Summary of New Ada Constructs

Construct				Effect

IF Statement

One Alternative

IF X /= 0.0 THEN			Multiplies Product by X only if X
  Product := Product * X;		is nonzero.
END IF;
Two Alternatives
IF X >= 0 THEN				If X is greater than or equal to 0,
  Ada.Integer.Text_IO.Put(Item=>X);	the message " is positive" is
  Ada.Text_IO.Put(" is positive");	displayed; otherwise, the message
ELSE	" is negative" is displayed.
  Ada.Integer.Text_IO.Put(Item=>X);
  Ada.Text_IO.Put(" is positive");
END IF;
Several Alternatives
IF X < 0.0 THEN				One of three messages is displayed
  Ada.Text_IO.Put(Item=>"negative"); 	depending on whether X is
  AbsX := -X;				negative, positive, or zero. 
ELSIF X = 0.0 THEN			AbsX is set to represent the
  Ada.Text_IO.Put(Item=>"zero");	absolute value or magnitude of X.
  AbsX := X;  
ELSE 
  Ada.Text_IO.Put(Item=>"positive");
  AbsX := X;
END IF; 
Function Specification
FUNCTION Sign (X :Float) RETURN Character;	specifies a function
Function Body
FUNCTION Sign (X :Float) RETURN Character IS
  Temp: Character;			Returns a character value that indicates 
BEGIN -- Sign				the sign ('+' or '-')
  IF X >= 0 THEN			of its type Float argument.
    Temp := '+';
  ELSE
    Temp := '-';
  END IF;
  RETURN Temp;
END Sign;

Quick-Check Exercises

  1. An IF statement implements __________ execution.
  2. What is pseudocode?
  3. What values can a Boolean expression have?
  4. The relational operator /= means _________.
  5. A _________ is used to verify that an algorithm is correct.
  6. What value is assigned to Fee by the IF statement below when speed is 75?
    IF Speed > 35 THEN
    	Fee := 20.0;
    ELSIF Speed > 50 THEN
    	Fee := 40.00;
    ELSIF Speed > 75 THEN
    	Fee := 60.00;
    END IF;
    
  7. Answer Quick-Check Exercise 6 for the IF statement below. Which IF statement is correct?
    IF Speed > 75 THEN
    	Fee := 60.0;
    ELSIF Speed > 50 THEN
    	Fee := 40.00;
    ELSIF Speed > 35 THEN
    	Fee := 20.00;
    END IF;
    
  8. What output line(s) are displayed by the statements below when X is 5.53? When X is 9.95?
    IF X >= 7.5 THEN
    	X := 90.0;
    	Ada.Float.Text_IO.Put(Item=>X, Fore=>2, Aft=>2, Exp=>0);
    ELSE
    	X := 25.0;
    	Ada.Float.Text_IO.Put(Item=>X, Fore=>2, Aft=>2, Exp=>0);
    END IF;
    
  9. Explain the difference between the statements on the left and the statements on the right below. For each of them, what is the final value of X if the initial value of X is 1?
    IF X >= 0 THEN			IF X >= 0 THEN 
    	X := X + 1;				X := X + 1;
    ELSIF X >= 1 THEN			END IF;
    	X := X + 2;			IF X >= 1 THEN
    END IF;					X := X + 2;
    					END IF;
    

Answers to Quick-Check Exercises

  1. Conditional
  2. A mixture of English and Ada used to describe algorithm steps
  3. True and False
  4. Not equal
  5. Hand trace
  6. 20.00, first condition is met
  7. 40.00, the one in 7
  8. When X is originally 5.53: 25.00; when X is originally 9.95: 90.00
  9. A multiple-alternative IF statement is on the left; a sequence of IF statements is on the right. X becomes 3 on the left; X becomes 4 on the right.

Review Questions for Chapter 4

  1. A decision in Ada is actually an evaluation of a(n) ____________ expression.
  2. List the six relational operators discussed in this chapter.
  3. What should the programmer do after writing the algorithm but before entering the program?
  4. Trace the following program fragment and indicate what will be displayed if a data value of 27.34 is entered.
    Text_IO.Put(Item => "Enter a temperature> ");
    Ada.Float.Text_IO.Get (Temp);
    IF Temp > 32.0 THEN
    	Ada.Text_IO.Put(Item => "Not Freezing");
    ELSE
    	Ada.Text_IO.Put(Item => "Ice Forming");
    END IF;
    
  5. Write the appropriate IF statement to compute GrossPay given that the hourly rate is stored in the variable Rate and the total hours worked is stored in the variable Hours. Pay time and a half for more than 40 hours worked.
  6. Explain the difference between a package specification and a package body.

Programming Projects

  1. Write a program to simulate a state police radar gun. The program should read an automobile's speed and print the message "speeding" if the speed exceeds 55 miles per hour.
  2. You need a program that will read a character value and a number. Depending on what is read, certain information will be displayed. The character should be either an S or a T. If an S is read and the number is, say, 100.50, the program will display
    Send money!  I need $100.50. 
    
    If a T is read instead of S, the program will display
    The temperature last night was 100.50 degrees. 
    
  3. Write a program that reads in a room number, its capacity, and the size of the class enrolled so far and displays an output line showing the classroom number, capacity, number of seats filled and available, and a message indicating whether the class is filled or not. Display the heading below before the output line.
    Room   Capacity   Enrollment   Empty seats  Filled/Not Filled
    
    Display each part of the output line under the appropriate column heading. Test your program with the following classroom data:
                     Room   Capacity    Enrollment
                      426      25          25
                      327      18          14
                      420      20          15
                      317     100          90
    
    
  4. Write a program that will determine the additional state tax owed by an employee. The state charges a 4% tax on net income. Determine net income by subtracting a $500 allowance for each dependent from gross income. Your program will read gross income, number of dependents, and tax amount already deducted. It will then compute the actual tax owed and display the difference between tax owed and tax deducted followed by the message "SEND CHECK" or "REFUND", depending on whether this difference is positive or negative.
  5. The New Telephone Company has the following rate structure for long-distance calls: Write a program that reads the start time for a call based on a 24-hour clock and the length of the call. The gross cost (before any discounts or tax) should be printed followed by the net cost (after discounts are deducted and tax is added).
  6. Write a program that uses package Min_Max to find the smallest and largest of four integers read from the terminal.


Previous | Next | Table of Contents | Index | Program List | Copyright

Copyright © 1996 by Addison-Wesley Publishing Company, Inc.