1 / 34

Chap 5 Control Structure

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

turnere
Télécharger la présentation

Chap 5 Control Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chap 5Control Structure Boolean Expression and the IF Statement

  2. IF Statement (One Alternative) IF condition THEN statement sequence T END IF; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if condition is true then execute the statement sequence T

  3. Example for IF statement IF X>=18 THEN Adult := Adult +1 END IF;

  4. IF Statement (Two Alternative) IF Condition THEN statement sequence T ELSE statement sequence F END IF; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  5. 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

  6. 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;

  7. Boolean Expression variable relational operator variable e.g. X > Y variable relational operator constant e.g. X >= 0.0

  8. Relational Operator <= less or equal to < less than >= greater than or equal to > greater than = equal to /= not equal to

  9. 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

  10. 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;

  11. 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;

  12. More Example Exam Score Grade ---------------- -------- 90 and above A 80-89 B 70-79 C 60-69 D below F

  13. 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

  14. 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

  15. 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;

  16. 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;

  17. 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);

  18. 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;

  19. Functions • Function Specification • Function Body

  20. FUNCTION SPECIFICATION FUNCTION Func_Name ( formal parameter) RETURN result _type; Example FUNCTION Maximum (VaL1, Val2 : Integer) RETURN Integer;

  21. FORM for FUNCTION BODY FUNCTION Func_Name (formal parameter) RETURN result_type IS local declaration section BEGIN statement sequence END Func_Name

  22. Example for FUNCTION BODY FUNCTION Square (Num : Integer) RETURN Integer IS Result : Integer; BEGIN Result := Num * Num; RETURN Result; END Square;

  23. 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;

  24. Calling A Function Score1 := 78; Score2 := 89; Larger := Maximum( VaL1=> Score1 ,VaL2 => Score2 ); What is the value of Large ?

  25. 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;

  26. 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;

  27. 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;

  28. Writing Package • Package Specification • Package Body

  29. Package Specification PACKAGE Pack_Name IS list of specifications of resourses provided by the package END Pack_Name ;

  30. Example forPackage Specification PACKAGE Min_Max IS FUNCTION Minimum ( VaL1 , VaL2 : Integer) RETURN Integer; FUNCTION Maximum ( VaL1 , VaL2 : Integer)RETURN Integer; END Min_Max;

  31. 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

  32. 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;

  33. 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;

  34. 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);

More Related