340 likes | 361 Views
Chap 5 Control Structure. Boolean Expression and the IF Statement. IF Statement (One Alternative). IF condition THEN statement sequence T END IF; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if condition is true then
E N D
Chap 5Control Structure Boolean Expression and the IF Statement
IF Statement (One Alternative) IF condition THEN statement sequence T END IF; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if condition is true then execute the statement sequence T
Example for IF statement IF X>=18 THEN Adult := Adult +1 END IF;
IF Statement (Two Alternative) IF Condition THEN statement sequence T ELSE statement sequence F END IF; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IF THEN ELSE if condition is true then execute statement sequence T and statement sequence F is skipped otherwise, statement sequence F is executed and statement sequence F is skipped
Example IF X >= 0.0 THEN Ada .TEXT_IO.Put (Item => “ X is Positive “); ELSE Ada .TEXT_IO.Put (Item => “ X is Negative”); END IF;
Boolean Expression variable relational operator variable e.g. X > Y variable relational operator constant e.g. X >= 0.0
Relational Operator <= less or equal to < less than >= greater than or equal to > greater than = equal to /= not equal to
Let us test how much we understand Suppose x, y , z : integer; if x := 2; y := 5; z := 0; Find the value for z after executing IF x >= y THEN z := z +1; ELSE z := z +2; END IF
The Multiple-AlternativeIF Statement IF condition-1 THEN statement sequence 1; ELSIF condition-2 THEN statement sequence 2; . . . . ELSIF condition-k THEN statement sequence k; ELSE statement sequence n; END IF;
Example IF N>= 0 THEN Ada.Text_IO.Put (Item=> “ Positive”); ELSIF N=0 THEN Ada.Text_IO.Put (Item => “Zero”); ELSE Ada.Text_IO.Put (Item => “Negative”); END IF;
More Example Exam Score Grade ---------------- -------- 90 and above A 80-89 B 70-79 C 60-69 D below F
Grade problem - - - grade assignment IF score >= 90 THEN Ada.Text_IO.Put ( Item => “ You got, A”); ELSIF score >= 80 THEN Ada.Text_IO.Put ( Item => “ You got, B”); ELSIF score >= 70 THEN Ada.Text_IO.Put ( Item => “ You got, C”); ELSIF score >= 60 THEN Ada.Text_IO.Put ( Item => “ You got, D”); ELSE Ada.Text_IO.Put ( Item => “ You got, F”); END IF
Think about this problem if you speed up in 35 mile limit zone Fees are as follow: $20 for under 50 mph, $40 for under 75 mph and $60 for more than 75 mph
Is This Answer “A” Ok? IF Speed > 35 THEN Fee := 20.00; ELSIF Speed >50 THEN Fee := 40.00 ELSIF Speed >75 THEN Fee := 60.00 END IF;
Or Is This Answer “B” OK ? IF Speed > 75 THEN Fee := 60.00; ELSIF Speed > 50THEN Fee := 40.00; ELSIF Speed > 35THEN Fee := 20.00; END IF;
Using Ada’s Math Library With Ada.Numerics.Elementary_Functions; Example Subtype NonNegFloat IS FLOAT Range 0.0 . . Float’Last; First : NonNegFloat; First:=Ada.Numerics.Elementary_Functions.Sqrt(X=> First);
Writing Functions Function Specifications In Chap 4, we found that Function Year (Date : Time) Return Year_Number; Function Month (Date: Time) Return Month_Number; Function Day (Date: Time) Return Day_Number;
Functions • Function Specification • Function Body
FUNCTION SPECIFICATION FUNCTION Func_Name ( formal parameter) RETURN result _type; Example FUNCTION Maximum (VaL1, Val2 : Integer) RETURN Integer;
FORM for FUNCTION BODY FUNCTION Func_Name (formal parameter) RETURN result_type IS local declaration section BEGIN statement sequence END Func_Name
Example for FUNCTION BODY FUNCTION Square (Num : Integer) RETURN Integer IS Result : Integer; BEGIN Result := Num * Num; RETURN Result; END Square;
Maximum Function FUNCTION Maximum ( VaL1, VaL2 : Integer) RETURN Integer IS Result : Integer; BEGIN IF VaL1 > VaL2 THEN Result := VaL1; ELSE Result := VaL2; End IF; RETURN Result; END Maximum;
Calling A Function Score1 := 78; Score2 := 89; Larger := Maximum( VaL1=> Score1 ,VaL2 => Score2 ); What is the value of Large ?
Example Program with Function With Ada.Text_IO; With Ada.Integer_Text_IO; Procedure Large Is - - This program is to find the maximum value of given two numbers - - - - Declare Variables Num1, Num2, Large : Integer;
Cont. Of Large_Small Program - - - - - - - -Function Specification FUNCTION Maximum (VaL1, Val2 : Integer) Return Integer; - - - - - - - - Function Body FUNCTION Maximum ( VaL1, VaL2 : Integer) RETURN Integer IS Result : Integer; BEGIN IF VaL1 > VaL2 THEN Result := VaL1; ELSE Result := VaL2; End IF; RETURN Result; END Maximum;
Cont.. Begin Ada.Text_IO.Put (Item =>" Give Number 1 : "); Ada.Integer_Text_IO.Get(Item => Num1); Ada.Text_IO.New_Line; Ada.Text_IO.Put (Item =>" Give Number 2 : "); Ada.Integer_Text_IO.Get(Item => Num2); Ada.Text_IO.New_Line; Large := Maximum( VaL1=>Num1, VaL2=>Num2); Ada.Text_IO.Put (Item => " Large Number is : "); Ada.Integer_Text_IO.Put(Item => Large); Ada.Text_IO.New_Line; End Large;
Writing Package • Package Specification • Package Body
Package Specification PACKAGE Pack_Name IS list of specifications of resourses provided by the package END Pack_Name ;
Example forPackage Specification PACKAGE Min_Max IS FUNCTION Minimum ( VaL1 , VaL2 : Integer) RETURN Integer; FUNCTION Maximum ( VaL1 , VaL2 : Integer)RETURN Integer; END Min_Max;
Form for Package Body PACKAGE BODY Pack_Name IS sequence of function and procedure bodies implementing the resources listed in the package specification for Pack_Name END Pack_Name
Example For Package Body PACKAGE BODY Min_Max IS - - - - - - - - bodies of functions for Min_Max package - - - - - - FUNCTION Minimum ( VaL1, VaL2 : Integer) RETURN Integer IS Result : Integer; BEGIN IF VaL1 <VaL2 THEN Result := VaL1; ELSE Result := VaL2; End IF; RETURN Result; END Minimum;
Cont… Of The Min_Max PACKAGE BODY FUNCTION Maximum ( VaL1, VaL2 : Integer) RETURN Integer IS Result : Integer; BEGIN IF VaL1 > VaL2 THEN Result := VaL1; ELSE Result := VaL2; End IF; RETURN Result; END Maximum; END Min_Max;
Calling Functions from Min_Max Package With Ada.Text_IO; With Ada.Integer_Text_IO; With Min_Max; - - - - - our new package Num1, Num2, Large, Small : Integer; Large := Min_Max.Maximum( VaL1=>Num1, VaL2=>Num2); Small := Min_Max.Minimum( VaL1=>Num1, VaL2=>Num2);