chap 7- control structures : the while statement
TRANSCRIPT
Chap 7- Control Structures :
The WHILE Statement
The WHILE Statement
The test in WHILE is opposite of the test in the general LOOP
Test the loop exit condition at the top of the loop
The loop body is repeated as long as the condition expression is true
Loop repetition stops when the conditon is false
In General Loop
PowerOf2 := 1;
LOOP
EXIT WHEN Power >= 10000;
Ada.Integer_Text_IO.Put (Item => PowerOf2);
PowerOf2 := PowerOf2 *2;
END LOOP;
In WHILE Loop
PowerOf2 := 1;
WHILE PowerOf2 < 10000 LOOP
Ada.Integer_Text_IO.Put (Item => PowerOf2);
PowerOf2 := PowerOf2 *2;
END LOOP;
Form
WHILE expression LOOP
statement sequence
END LOOP;
Robust Exception Handling
A Robust Ada Program
retain control and behave predictably even exceptions are raised
Template for a Robust Input Loop- Initial Version
LOOP Prompt the user for an input value Get the input value from the user EXIT the loop if and only if no exception
was raised on input If an exception was raised, notify the user
END LOOP
EXIT statement
Meaningful statement only within a loop structure
Transfer control to the next statement after the nearest END LOOP
Template for a Robust Input Loop- Refined VersionLOOP
BEGIN
Prompt the user for an input value
Get the input value from the user
EXIT; -- valid data
EXCEPTION -- invalid data
Determine which exception was raised, and notify the user
END
END LOOP
Form for Exception Handler
WHEN exception name =>
sequence of statements
Example,
WHEN Constraint_Error =>
Ada.Text_IO.Put(Item=>”Input number is out of range”);
Ada.Text_IO.New_Line;
Ada.Text_IO.Put(Item=>”Input number is out of range”);
Ada.Text_IO.New_Line;
Exception Handler BlockForm:Begin
normal sequence of statements
EXCEPTION
WHEN exception name1 =>
sequence of statements1
WHEN exception name2 =>
sequence of statements2….
WHEN exception nameN =>
sequence of statementsN
END;
An Example of Robust Numeric Input
WITH Ada.Text_IO;WITH Ada.Integer_Text_IO;PROCEDURE Exception_Loop IS----------------------------------------------------------------| Illustrates how to write a robust input loop that--| prompts user to re-enter invalid input and--| refuses to continue until input is good.-------------------------------------------------------------- MinVal : CONSTANT Integer := -10; MaxVal : CONSTANT Integer := 10; SUBTYPE SmallInt IS Integer RANGE MinVal .. MaxVal; InputValue: SmallInt; Sum: Integer;
BEGIN -- Exception_Loop Sum := 0;
FOR Count IN 1..5 LOOP LOOP -- inner loop just to control robust input BEGIN -- block for exception handler Ada.Text_IO.Put(Item => "Enter an integer between "); Ada.Integer_Text_IO.Put (Item => SmallInt'First, Width => 0); Ada.Text_IO.Put(Item => " and "); Ada.Integer_Text_IO.Put (Item => SmallInt'Last, Width => 0); Ada.Text_IO.Put(Item => " > "); Ada.Integer_Text_IO.Get(Item => InputValue); EXIT; -- leave the loop only upon correct input
EXCEPTION WHEN Constraint_Error => Ada.Text_IO.Put ("Value is out of range. Please try again."); Ada.Text_IO.New_Line; WHEN Ada.Text_IO.Data_Error => Ada.Text_IO.Put ("Value is not an integer. Please try again."); Ada.Text_IO.New_Line; Ada.Text_IO.Skip_Line; END; -- block for exception handler END LOOP; Sum := Sum + InputValue; -- add new value into Sum END LOOP;
Ada.Text_IO.Put (Item => "The sum is "); Ada.Integer_Text_IO. Put (Item => Sum, Width =>
1); Ada.Text_IO.New_Line;
END Exception_Loop;
Procedure
Procedures and functions are both subprograms, But
A procedure is called with a procedure call A procedure does not return a result A procedure is allowed to have parameters of three
kinds or modes:– IN parameter– OUT parameter– IN OUT parameter
IN parameters
– are passed into the procedure and, inside the procedure, they are treated as constants and may not be changed
OUT Parameters
– are computed in the procedure and passed out to the caller
IN OUT parameters
– are passed into the procedure, possibly changed by it, and passed back out again.
Sort Three Numbers
Suppose we have a procedure called Order ‘Order’ procedure to order the values of
value1 and value2 After the procedure call, value1 and value2
are in order
For given three numbers
Num1,Num2, Num3
order Num1 and Num2 order Num1 and Num3 order Num2 and Num3 Now every thing is in order
Example 1
For 7, 5, 3 order 7 and 5, then it becomes 5, 7, and 3 order 5 and 3, then it becomes 3, 7, and 5 order 7 and 5, then it becomes 3, 5, and 7that is, order Num1 and Num2, then it becomes 5, 7, and 3 order Num1 and Num3, then it becomes 3, 7, and 5 order Num2 and Num3, then it becomes 3, 5, and 7
Example 2
For 5, 3, 7 order 5 and 3, then it becomes 3, 5 and 7 order 3 and 7, then it remains 3, 5, and 7 order 5 and 7, then it remains 3, 5 and 7that is, order Num1 and Num2, then it becomes 3, 5, and 7 order Num1 and Num3, then it becomes 3, 5, and 7 order Num2 and Num3, then it becomes 3, 5, and 7
Example 3
For 5, 7, 3 order 5 and 7, then it becomes 5, 7 and 3 order 5 and 3, then it becomes 3, 7, and 5 order 7 and 5, then it becomes 3, 5 and 7that is, order Num1 and Num2, then it becomes 5, 7, and 3 order Num1 and Num3, then it becomes 3, 7, and 5 order Num2 and Num3, then it becomes 3, 5, and 7
Order Procedure
PROCEDURE Order (X: IN OUT Float; Y: IN OUT Float) IS -- Pre: X and Y are assigned values. -- Post: X has the smaller value and Y has the larger
value. Temp : Float; -- copy of number originally in
X BEGIN -- Order IF X > Y THEN -- interchange the values of X and Y Temp := X; -- Store old X in Temp X := Y; -- Store old Y in X Y := Temp; -- Store old X in Y END IF; END Order;
Example - using order procedure WITH Ada.Text_IO;WITH Ada.Float_Text_IO;PROCEDURE Sort_3_Numbers IS----------------------------------------------------------------| Reads three numbers and sorts them--| so that they are in increasing order.--------------------------------------------------------------
--
Num1 : Float; -- a list of three cells Num2 : Float; Num3 : Float;
Cont. sort program
-- Procedure is like function-- It is required to write Specification and Body---- procedure specification PROCEDURE Order (X: IN OUT Float; Y: IN OUT Float);
Cont. sort program
-- procedure body PROCEDURE Order (X: IN OUT Float; Y: IN OUT Float) IS -- Pre: X and Y are assigned values. -- Post: X has the smaller value and Y has the larger value. Temp : Float; -- copy of number originally in X
-- Local variable BEGIN -- Order IF X > Y THEN -- interchange the values of X and Y Temp := X; -- Store old X in Temp X := Y; -- Store old Y in X Y := Temp; -- Store old X in Y END IF; END Order;
Cont. sort program
BEGIN -- Sort_3_Numbers Ada.Text_IO.Put (Item => "Enter 3 float values to be sorted>"); Ada.Text_IO.New_Line; Ada.Float_Text_IO.Get(Item => Num1); Ada.Float_Text_IO.Get(Item => Num2); Ada.Float_Text_IO.Get(Item => Num3); -- Sort the numbers Order (X => Num1, Y => Num2); Order (X => Num1, Y => Num3); Order (X => Num2, Y => Num3);
Cont. sort program
-- Display the results. Ada.Text_IO.Put(Item => "The three numbers in order
are: "); Ada.Float_Text_IO.Put (Item => Num1, Fore => 5, Aft => 2, Exp => 0); Ada.Float_Text_IO.Put (Item => Num2, Fore => 5, Aft => 2, Exp => 0); Ada.Float_Text_IO.Put (Item => Num3, Fore => 5, Aft => 2, Exp => 0); Ada.Text_IO.New_Line;
END Sort_3_Numbers;
Procedure Specification
Form:
PROCDURE PNAME (FORMAL-PARAMETERS);
--------------------------------------------------------------------------
Example:
Procedure Double(X : IN Integer; Y: OUT Integer);
Procedure Body
Form:
PROCDURE PNAME (FORMAL-PARAMETERS) IS
local declaration-section;
Begin
Statement sequence
End PNAME;
Example:
Procedure Double(X : IN Integer; Y: OUT Integer) IS
Begin
Y := 2 * X;
End Double;
Procedure Call
Form:
PNAME ( actual-parameters);
Example:
Double ( X => P, Y => Q );
Rules for Parameter List Correspondence
Formal and actual parameter correspondence by named association or by position by type
Actual parameter variable for OUT or IN OUT parameter mode variable, constant, or expression for IN
parameter mode
Style
Naming is used to associate each formal parameter with an actual parameter (optional)
Order of actual parameters does not have to match the order of the formal parameters
But, it is a good practice to use
named association
list the actual parameters in an order corresponding to the order of the formal parameters