Download presentation
Presentation is loading. Please wait.
Published byEarl Chandler Modified over 9 years ago
1
Chap 5 Control Structure Boolean Expression and the IF Statement
2
IF Statement (One Alternative) IF condition THEN statement sequence T statement sequence T END IF; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if condition is true then if condition is true then execute the statement sequence T execute the statement sequence T
3
Example for IF statement IF X>=18 THEN Adult := Adult +1 Adult := Adult +1 END IF;
4
IF Statement (Two Alternative) IF Condition THEN statement sequence T statement sequence TELSE statement sequence F 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 “); Ada.TEXT_IO.Put (Item => “ X is Positive “);ELSE Ada.TEXT_IO.Put (Item => “ X is Negative”); Ada.TEXT_IO.Put (Item => “ X is Negative”); END IF;
7
Boolean Expression variable relational operator variable e.g. X > Y X > Y variable relational operator constant e.g. X >= 0.0 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; if x := 2; y := 5; y := 5; z := 0; z := 0; Find the value for z after executing IF x >= y THEN IF x >= y THEN z := z +1; z := z +1; ELSE ELSE z := z +2; z := z +2; END IF END IF
10
The Multiple-Alternative IF Statement IF condition-1 THEN statement sequence 1; statement sequence 1; ELSIF condition-2 THEN statement sequence 2; statement sequence 2;........ ELSIF condition-k THEN statement sequence k; statement sequence k;ELSE statement sequence n; statement sequence n; END IF;
11
Example IF N>= 0 THEN Ada.Text_IO.Put (Item=> “ Positive”); Ada.Text_IO.Put (Item=> “ Positive”); ELSIF N=0 THEN Ada.Text_IO.Put (Item => “Zero”); Ada.Text_IO.Put (Item => “Zero”);ELSE Ada.Text_IO.Put (Item => “Negative”); Ada.Text_IO.Put (Item => “Negative”); END IF;
12
More Example Exam ScoreGrade ------------------------ 90 and aboveA 80-89B 70-79C 60-69D belowF
13
Grade problem - - - grade assignment IF score >= 90 THEN Ada.Text_IO.Put ( Item => “ You got, A”); Ada.Text_IO.Put ( Item => “ You got, A”); ELSIF score >= 80 THEN Ada.Text_IO.Put ( Item => “ You got, B”); Ada.Text_IO.Put ( Item => “ You got, B”); ELSIF score >= 70 THEN Ada.Text_IO.Put ( Item => “ You got, C”); Ada.Text_IO.Put ( Item => “ You got, C”); ELSIF score >= 60 THEN Ada.Text_IO.Put ( Item => “ You got, D”); Ada.Text_IO.Put ( Item => “ You got, D”);ELSE Ada.Text_IO.Put ( Item => “ You got, F”); 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: Fees are as follow: $20 for under 50 mph, $20 for under 50 mph, $40 for under 75 mph and $40 for under 75 mph and $60 for more than 75 mph $60 for more than 75 mph
15
Is This Answer “A” Ok? IF Speed > 35 THEN Fee := 20.00; Fee := 20.00; ELSIF Speed >50 THEN Fee := 40.00 Fee := 40.00 ELSIF Speed >75 THEN Fee := 60.00 Fee := 60.00 END IF;
16
Or Is This Answer “B” OK ? IF Speed > 75 THEN Fee := 60.00; Fee := 60.00; ELSIF Speed > 50THEN Fee := 40.00; Fee := 40.00; ELSIF Speed > 35THEN Fee := 20.00; 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 b Function Specification b Function Body
20
FUNCTION SPECIFICATION FUNCTION Func_Name ( formal parameter) RETURN result _type; RETURN result _type;Example FUNCTION Maximum (VaL1, Val2 : Integer) RETURN Integer; RETURN Integer;
21
FORM for FUNCTION BODY FUNCTION Func_Name (formal parameter) RETURN result_type IS local declaration section RETURN result_type IS local declaration section BEGIN BEGIN statement sequence statement sequence END Func_Name END Func_Name
22
Example for FUNCTION BODY FUNCTION Square (Num : Integer) RETURN Integer IS Result : Integer; Result : Integer; BEGIN BEGIN Result := Num * Num; Result := Num * Num; RETURN Result; RETURN Result; END Square; END Square;
23
Maximum Function FUNCTION Maximum ( VaL1, VaL2 : Integer) RETURN Integer IS Result : Integer; BEGIN IF VaL1 > VaL2 THEN IF VaL1 > VaL2 THEN Result := VaL1; Result := VaL1; ELSE ELSE Result := VaL2; 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 IF VaL1 > VaL2 THEN Result := VaL1; Result := VaL1; ELSE ELSE Result := VaL2; Result := VaL2; End IF; End IF; RETURN Result; 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 b Package Specification b Package Body
29
Package Specification PACKAGE Pack_Name IS list of specifications of resourses list of specifications of resourses provided by the package provided by the package END Pack_Name ;
30
Example for Package Specification PACKAGE Min_Max IS FUNCTION Minimum ( VaL1, VaL2 : Integer) RETURN Integer; FUNCTION Minimum ( VaL1, VaL2 : Integer) RETURN Integer; FUNCTION Maximum ( 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 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 IF VaL1 <VaL2 THEN Result := VaL1; Result := VaL1; ELSE ELSE Result := VaL2; Result := VaL2; End IF; End IF; RETURN Result; 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 IF VaL1 > VaL2 THEN Result := VaL1; Result := VaL1; ELSE ELSE Result := VaL2; Result := VaL2; End IF; End IF; RETURN Result; 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);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.