Download presentation
Presentation is loading. Please wait.
1
Chap 7- Control Structures : The WHILE Statement
2
n The test in WHILE is opposite of the test in the general LOOP n Test the loop exit condition at the top of the loop n The loop body is repeated as long as the condition expression is true n Loop repetition stops when the conditon is false
3
In General Loop PowerOf2 := 1; LOOP EXIT WHEN Power >= 10000; Ada.Integer_Text_IO.Put (Item => PowerOf2); PowerOf2 := PowerOf2 *2; END LOOP ;
4
In WHILE Loop PowerOf2 := 1; WHILE PowerOf2 < 10000 LOOP Ada.Integer_Text_IO.Put (Item => PowerOf2); PowerOf2 := PowerOf2 *2; END LOOP ;
5
Form WHILE expression LOOP statement sequence END LOOP;
6
Robust Exception Handling A Robust Ada Program n retain control and behave predictably even exceptions are raised
7
Template for a Robust Input Loop - Initial Version LOOP n Prompt the user for an input value n Get the input value from the user n EXIT the loop if and only if no exception was raised on input n If an exception was raised, notify the user END LOOP
8
EXIT statement n Meaningful statement only within a loop structure n Transfer control to the next statement after the nearest END LOOP
9
Template for a Robust Input Loop - Refined Version LOOP 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
10
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;
11
Exception Handler Block Form: 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;
12
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;
13
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
14
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;
15
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;
16
Procedure n Procedures and functions are both subprograms, But n A procedure is called with a procedure call n A procedure does not return a result n A procedure is allowed to have parameters of three kinds or modes: – IN parameter – OUT parameter – IN OUT parameter
17
IN parameters – are passed into the procedure and, inside the procedure, they are treated as constants and may not be changed
18
OUT Parameters – are computed in the procedure and passed out to the caller
19
IN OUT parameters – are passed into the procedure, possibly changed by it, and passed back out again.
20
Sort Three Numbers n Suppose we have a procedure called Order n ‘Order’ procedure to order the values of value1 and value2 n After the procedure call, value1 and value2 are in order
21
For given three numbers Num1,Num2, Num3 n order Num1 and Num2 n order Num1 and Num3 n order Num2 and Num3 n Now every thing is in order
22
Example 1 For7, 5, 3 n order 7 and 5, then it becomes 5, 7, and 3 n order 5 and 3, then it becomes 3, 7, and 5 n order 7 and 5, then it becomes 3, 5, and 7 that is, n order Num1 and Num2, then it becomes 5, 7, and 3 n order Num1 and Num3, then it becomes 3, 7, and 5 n order Num2 and Num3, then it becomes 3, 5, and 7
23
Example 2 For 5, 3, 7 n order 5 and 3, then it becomes 3, 5 and 7 n order 3 and 7, then it remains 3, 5, and 7 n order 5 and 7, then it remains 3, 5 and 7 that is, n order Num1 and Num2, then it becomes 3, 5, and 7 n order Num1 and Num3, then it becomes 3, 5, and 7 n order Num2 and Num3, then it becomes 3, 5, and 7
24
Example 3 For5, 7, 3 n order 5 and 7, then it becomes 5, 7 and 3 n order 5 and 3, then it becomes 3, 7, and 5 n order 7 and 5, then it becomes 3, 5 and 7 that is, n order Num1 and Num2, then it becomes 5, 7, and 3 n order Num1 and Num3, then it becomes 3, 7, and 5 n order Num2 and Num3, then it becomes 3, 5, and 7
25
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;
26
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;
27
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);
28
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;
29
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);
30
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;
31
Procedure Specification Form: PROCDURE PNAME (FORMAL-PARAMETERS); -------------------------------------------------------------------------- Example: Procedure Double(X : IN Integer; Y: OUT Integer);
32
Procedure Body Form: PROCDURE PNAME (FORMAL-PARAMETERS) IS local declaration-section; Begin Statement sequence End PNAME;
33
Example: Procedure Double(X : IN Integer; Y: OUT Integer) IS Begin Y := 2 * X; End Double;
34
Procedure Call Form: PNAME ( actual-parameters); Example: Double ( X => P, Y => Q );
35
Rules for Parameter List Correspondence Formal and actual parameter correspondence n by named association or by position n by type Actual parameter n variable for OUT or IN OUT parameter mode n variable, constant, or expression for IN parameter mode
36
Style n Naming is used to associate each formal parameter with an actual parameter (optional) n Order of actual parameters does not have to match the order of the formal parameters
37
But, it is a good practice to use n named association n list the actual parameters in an order corresponding to the order of the formal parameters
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.