Chap 5 Control Structure - PowerPoint PPT Presentation

About This Presentation
Title:

Chap 5 Control Structure

Description:

Chap 5 Control Structure Boolean Expression and the IF Statement – PowerPoint PPT presentation

Number of Views:136
Avg rating:3.0/5.0
Slides: 35
Provided by: mht5
Category:

less

Transcript and Presenter's Notes

Title: Chap 5 Control Structure


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 Xgt18 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 gt 0.0 THEN
  • Ada .TEXT_IO.Put (Item gt X is Positive )
  • ELSE
  • Ada .TEXT_IO.Put (Item gt X is Negative)
  • END IF

7
Boolean Expression
  • variable relational operator variable
  • e.g.
  • X gt Y
  • variable relational operator constant
  • e.g.
  • X gt 0.0

8
Relational Operator
  • lt less or equal to
  • lt less than
  • gt greater than or equal to
  • gt 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 gt 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 Ngt 0 THEN
  • Ada.Text_IO.Put (Itemgt Positive)
  • ELSIF N0 THEN
  • Ada.Text_IO.Put (Item gt Zero)
  • ELSE
  • Ada.Text_IO.Put (Item gt 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 gt 90 THEN
  • Ada.Text_IO.Put ( Item gt You got,
    A)
  • ELSIF score gt 80 THEN
  • Ada.Text_IO.Put ( Item gt You got,
    B)
  • ELSIF score gt 70 THEN
  • Ada.Text_IO.Put ( Item gt You got, C)
  • ELSIF score gt 60 THEN
  • Ada.Text_IO.Put ( Item gt You got, D)
  • ELSE
  • Ada.Text_IO.Put ( Item gt 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 gt 35 THEN
  • Fee 20.00
  • ELSIF Speed gt50 THEN
  • Fee 40.00
  • ELSIF Speed gt75 THEN
  • Fee 60.00
  • END IF

16
Or Is This Answer B OK ?
  • IF Speed gt 75 THEN
  • Fee 60.00
  • ELSIF Speed gt 50THEN
  • Fee 40.00
  • ELSIF Speed gt 35THEN
  • Fee 20.00
  • END IF

17
Using Adas Math Library
  • With Ada.Numerics.Elementary_Functions
  • Example
  • Subtype NonNegFloat IS FLOAT Range 0.0 . .
    FloatLast
  • First NonNegFloat
  • FirstAda.Numerics.Elementary_Functions.Sqrt(Xgt
    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 gt VaL2 THEN
  • Result VaL1
  • ELSE
  • Result VaL2
  • End IF
  • RETURN Result
  • END Maximum

24
Calling A Function
  • Score1 78
  • Score2 89
  • Larger Maximum( VaL1gt Score1 ,VaL2 gt 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 gt VaL2 THEN
  • Result VaL1
  • ELSE
  • Result VaL2
  • End IF
  • RETURN Result
  • END Maximum

27
Cont..
  • Begin
  • Ada.Text_IO.Put (Item gt" Give Number 1 ")
  • Ada.Integer_Text_IO.Get(Item gt Num1)
  • Ada.Text_IO.New_Line
  • Ada.Text_IO.Put (Item gt" Give Number 2 ")
  • Ada.Integer_Text_IO.Get(Item gt Num2)
  • Ada.Text_IO.New_Line
  • Large Maximum( VaL1gtNum1, VaL2gtNum2)
  • Ada.Text_IO.Put (Item gt " Large Number is ")
  • Ada.Integer_Text_IO.Put(Item gt 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 ltVaL2 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 gt 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( VaL1gtNum1,
    VaL2gtNum2)
  • Small Min_Max.Minimum( VaL1gtNum1,
    VaL2gtNum2)
Write a Comment
User Comments (0)
About PowerShow.com