![Page 1: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/1.jpg)
Chap 7- Control Structures :
The WHILE Statement
![Page 2: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/2.jpg)
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
![Page 3: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/3.jpg)
In General Loop
PowerOf2 := 1;
LOOP
EXIT WHEN Power >= 10000;
Ada.Integer_Text_IO.Put (Item => PowerOf2);
PowerOf2 := PowerOf2 *2;
END LOOP;
![Page 4: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/4.jpg)
In WHILE Loop
PowerOf2 := 1;
WHILE PowerOf2 < 10000 LOOP
Ada.Integer_Text_IO.Put (Item => PowerOf2);
PowerOf2 := PowerOf2 *2;
END LOOP;
![Page 5: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/5.jpg)
Form
WHILE expression LOOP
statement sequence
END LOOP;
![Page 6: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/6.jpg)
Robust Exception Handling
A Robust Ada Program
retain control and behave predictably even exceptions are raised
![Page 7: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/7.jpg)
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
![Page 8: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/8.jpg)
EXIT statement
Meaningful statement only within a loop structure
Transfer control to the next statement after the nearest END LOOP
![Page 9: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/9.jpg)
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
![Page 10: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/10.jpg)
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;
![Page 11: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/11.jpg)
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;
![Page 12: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/12.jpg)
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;
![Page 13: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/13.jpg)
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
![Page 14: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/14.jpg)
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;
![Page 15: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/15.jpg)
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;
![Page 16: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/16.jpg)
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
![Page 17: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/17.jpg)
IN parameters
– are passed into the procedure and, inside the procedure, they are treated as constants and may not be changed
![Page 18: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/18.jpg)
OUT Parameters
– are computed in the procedure and passed out to the caller
![Page 19: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/19.jpg)
IN OUT parameters
– are passed into the procedure, possibly changed by it, and passed back out again.
![Page 20: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/20.jpg)
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
![Page 21: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/21.jpg)
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
![Page 22: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/22.jpg)
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
![Page 23: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/23.jpg)
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
![Page 24: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/24.jpg)
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
![Page 25: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/25.jpg)
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;
![Page 26: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/26.jpg)
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;
![Page 27: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/27.jpg)
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);
![Page 28: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/28.jpg)
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;
![Page 29: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/29.jpg)
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);
![Page 30: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/30.jpg)
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;
![Page 31: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/31.jpg)
Procedure Specification
Form:
PROCDURE PNAME (FORMAL-PARAMETERS);
--------------------------------------------------------------------------
Example:
Procedure Double(X : IN Integer; Y: OUT Integer);
![Page 32: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/32.jpg)
Procedure Body
Form:
PROCDURE PNAME (FORMAL-PARAMETERS) IS
local declaration-section;
Begin
Statement sequence
End PNAME;
![Page 33: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/33.jpg)
Example:
Procedure Double(X : IN Integer; Y: OUT Integer) IS
Begin
Y := 2 * X;
End Double;
![Page 34: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/34.jpg)
Procedure Call
Form:
PNAME ( actual-parameters);
Example:
Double ( X => P, Y => Q );
![Page 35: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/35.jpg)
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
![Page 36: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/36.jpg)
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
![Page 37: Chap 7- Control Structures : The WHILE Statement](https://reader035.vdocument.in/reader035/viewer/2022062321/56649f205503460f94c38509/html5/thumbnails/37.jpg)
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