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.
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 functionFunction 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;
IF
statement implements __________ execution.
/=
means _________.
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;
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;
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;
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;
X
is originally 5.53
: 25.00
;
when X
is originally 9.95
: 90.00
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.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;
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.
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.
Room Capacity Enrollment Empty seats Filled/Not FilledDisplay 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
"SEND
CHECK"
or "REFUND"
, depending on whether this difference is
positive or negative.
Min_Max
to find the
smallest and largest of four integers read from the terminal.
Copyright © 1996 by Addison-Wesley Publishing Company, Inc.