Lecture (8)Decision Making

41
1 UNIVERSITI KUALA LUMPUR Malaysia France Institute FSB23103 FSB23103 Object Oriented Programming Lecture 9 Decision making - IF and SELECT statements Mdm Ratnawati Ibrahim

Transcript of Lecture (8)Decision Making

Page 1: Lecture (8)Decision Making

1

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

FSB23103 Object Oriented Programming

Lecture 9

Decision making -IF and SELECT statements

Mdm Ratnawati Ibrahim

Page 2: Lecture (8)Decision Making

2

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Topics

• Program control• Control structures• Flow diagram • Selection structures

IF Select

Page 3: Lecture (8)Decision Making

3

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

VB Program

• Examples

The order in which program codes are executed is important

Dim a As Integer = 2Dim b As Integer = 1Dim c As Integer

c = a + bb = a + 2

2a

1b

3c

4b

Dim a As Integer = 2Dim b As Integer = 1Dim c As Integer

b = a + 2c = a + b

2a

4b

6c

1b

Page 4: Lecture (8)Decision Making

4

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Program control

• A program control is to specify the order in which program codes are executed

• A computing problem is usually solved by executing a series of actions in a specific order. This sequence is called an algorithm

Page 5: Lecture (8)Decision Making

5

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Control Structures

• Three control structures: sequence selection repetition

Page 6: Lecture (8)Decision Making

6

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Flow diagram

• A schematic representation of an algorithm in a computer program

• e.g.

testcondition

statements

statements

Exit

Start

false

true

Repetition

Selection

Sequence

(To be covered later)

Page 7: Lecture (8)Decision Making

7

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Sequence

• In a VB program, statements are executed in a sequential order

• e.g.

• Flow diagram Declare variables a, b, c

Assign a + 2 to b

Assign a + b to c

Dim a As Integer = 2Dim b As Integer = 1Dim c As Integer

b = a + 2c = a + b

Page 8: Lecture (8)Decision Making

8

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Selection (1)

• To make a decision based on the result of a test condition

• e.g. Assign a+2 to b only if a is greater than 5

Declare variables a, b, c

Assign a + 2 to b

If a > 5true

false

Assign a + b to c

Page 9: Lecture (8)Decision Making

9

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Selection (2)

• In VB .Net, use IF statement • e.g.

Dim a As Integer = 2Dim b As Integer = 1Dim c As Integer

If a > 5 Then b = a + 2End If

c = a + b

IF

2a

3c

1b

Question: What value does c contain if a is initialized as 6 instead?

Page 10: Lecture (8)Decision Making

10

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Selection (3)

• e.g.

Dim a As Integer = 6Dim b As Integer = 1Dim c As Integer

If a > 5 Then b = a + 2End If

c = a + b

IF

6a

14c

1b

8b

Page 11: Lecture (8)Decision Making

11

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Selection types

• Types of selection structure in VB .Net: IF IF … Else Select

Page 12: Lecture (8)Decision Making

12

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

IF statement (1)

• If condition Then action statements End If

Execute these statements if the condition is met, i.e. equals to True

Page 13: Lecture (8)Decision Making

13

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

IF statement (2)

• e.g. variable found is declared as Boolean

• Flow diagram

If found Then MessageBox.Show(”Found it!”)End If

If found is

Show message ”Found it!”

true

false

The value of a Boolean variable is either True or False

Page 14: Lecture (8)Decision Making

14

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

IF … Else statement (1)

• If condition Then statements Else statements

End If

Execute these statements if the condition is met, i.e. equals to True

Execute these statements if the condition is not met, i.e. equals to False

Page 15: Lecture (8)Decision Making

15

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

IF … Else statement (2)

• e.g.

If found is

Show message ”Found it!”

true

false

Show message ”It doesn’t exists!”

If found Then MessageBox.Show(”Found it!”)Else MessageBox.Show(”It doesn’t exist!”)End If

IF

ELSE

Page 16: Lecture (8)Decision Making

16

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Nested IFs (1)

• Multiple cases may be handled by nestingIF or IF ... ELSE statements

• e.g. If mark >= 40

• show message “You’ve passed the exam”

• If mark >= 70show another message “with distinction!”

If mark < 40 show message “Sorry, you failed the exam.”

Page 17: Lecture (8)Decision Making

17

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Nested IFs (2)

If mark >= 40

MessageBox.Show(”You’ve passed the exam”)

true

false

MessageBox.Show(”Sorry, you failed the exam”)

Mark >= 70

MessageBox.Show(” with distinction!”)

true

false

• Flow diagram

Nested IF

Page 18: Lecture (8)Decision Making

18

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Nested IFs (3)

• VB code

If mark >= 40 Then MessageBox.Show(”You’ve passed the exam”) If mark >= 70 then MessageBox.Show(” with distinction!”) End ifElse MessageBox.Show(”Sorry, you failed the exam.”)End If

Nested IF

Page 19: Lecture (8)Decision Making

19

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

ElseIf (1)

mark >= 70

Result is ”Distinction”

false

true Mark >= 60

true

false

Mark >= 40

Result is ”Merit”

truefalse

Result is ”Pass”

Result is ”Fail”

mutually exclusive choices Mark Result >= 70 Distinction 60-69 Merit 40-59 Pass < 40 Fail

Page 20: Lecture (8)Decision Making

20

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

ElseIf (2)

• To describe a series of mutually exclusive choices, use ElseIf

• e.g.

If mark >= 70 Then lblResult.Text = “Distinction”ElseIf mark >= 60 Then lblResult.Text= “Merit”ElseIf mark >= 40 Then lblResult.Text = “Pass”Else lblResult.Text = “Fail”End If

Note: There is only one End If statement.

ElseIF

Page 21: Lecture (8)Decision Making

21

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Comparison/relational operators

Operator Example Interpretation

= A = B A is equal to B

<> A < > B A is not equal to B

> A > B A is greater than B

< A < B A is less than B

>= A >= B A is greater than or equal to B

<= A <= B A is less than or equal to B

They are used to form simple conditions

Page 22: Lecture (8)Decision Making

22

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Logical Operators (1)

• They are used to form more complex conditions by combining simple conditions

Operator Example Value

AND A AND B True if both A and B are true, otherwise False

OR A OR B True if either A or B is true; False if both A and B are false

NOT NOT A Reverse A

Page 23: Lecture (8)Decision Making

23

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Logical Operators

• e.g.

If (mark >= 60) AND (mark < 70) Then lblResult.Text= ”Merit”End If

AND

If (module=”CO331”) OR (module=”CO332”) Then dept = ”Computer Science”End If

OR

If NOT (mark < 40) Then lblResult.Text= ”Pass”End If

NOT

Page 24: Lecture (8)Decision Making

24

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example - multiple cases (1)

• e.g. k is an Integer between 0 and 4,the value the String variable weekDay depends on the value of k weekDay = “Monday” if k=0 weekDay = “Tuesday” if k=1 weekDay = “Wednesday” if k=2 weekDay = “Thursday” if k=3 weekDay = “Friday” if k=4

Page 25: Lecture (8)Decision Making

25

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example - multiple cases (2)

k = 0

weekDay=”Monday”

false

truek = 1

true

false

k = 2

weekDay=”Tuesday”

true false

k = 3false

k = 4weekDay=”Wednesday” true

weekDay=”Thurday” true

weekDay=”Friday”

false

Page 26: Lecture (8)Decision Making

26

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example - multiple cases (3)

• To implement in VB, you may use ElseIf • Alternatively, use Select statement

Page 27: Lecture (8)Decision Making

27

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example - multiple cases (4)

• Program code

Select Case kCase 0

weekDay = ”Monday” Case 1

weekDay = ”Tuesday” Case 2

weekDay = ”Wednesday” Case 3

weekDay = ”Thursday” Case 4

weekDay = ”Friday”End Select

Page 28: Lecture (8)Decision Making

28

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Select statement (1)

Select Case selector

Case value1statements

Case value2statements

………..Case Else

statements

End Select

Action statements if selector=value1

Action statements if selector=value2

Action statements if all cases are false

Page 29: Lecture (8)Decision Making

29

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Select statement (2)

• The selector can contain any combination of variables, constants, functions, operators and properties

• The data type of value1, value2,…, etc. must be compatible with the data type of selector

• The final Case Else statement is optional• The structure must be terminated with End

Select

Page 30: Lecture (8)Decision Making

30

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Select statement (3)

• Example - range of selector value

Function GetResult(ByVal mark As Integer) As String Select Case mark Case 70 To 100 Return "Distinction" Case 60 To 69 Return "Merit" Case 40 To 59 Return "Pass" Case 0 To 40 Return "Fail" Case Else Return CStr(mark) & " is not a valid mark." End SelectEnd Function

Function GetResult

Mark Result 70-100 Distinction 60-69 Merit 40-59 Pass 0-39 FailOthers Error message

Page 31: Lecture (8)Decision Making

31

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example – module marks (1)

• Assuming the overall mark is a weighted average of 40% of coursework mark 60% of exam mark

• If the overall mark is 70 – 100 - Distinction 60 – 69 - Merit 40 – 59 - Pass 0 – 39 - Fail

Page 32: Lecture (8)Decision Making

32

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example - module marks (2)

Module Mark

Page 33: Lecture (8)Decision Making

33

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example - module marks (3)

• Program specification Input

• coursework weighting %• coursework mark • exam mark

Output• Display the overall mark and final result

Distinction (>= 70) Merit (between

60 and 69) Pass (between

40 and 59) Fail (< 40)

Page 34: Lecture (8)Decision Making

34

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example - module marks (3)

• Form design

TextBoxtxbCwkWeighting

TextBoxtxbExamMark

TextBoxtxbCwkMark

ButtoncmdCalculate

LabellblResult

ButtoncmdClear

ButtoncmdClose

Page 35: Lecture (8)Decision Making

35

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example - module marks (2)

• Flow diagram

Output results

Get inputs

Do calculation

Check if inputs are valid

Handle invalid inputs

True

False

Page 36: Lecture (8)Decision Making

36

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example - module marks (4)

Private Sub cmdCalculate_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdCalculate.Click Dim cwkWeighting As Integer = GetNumber(txbCwkWeighting.Text) Dim cwkMark As Integer = GetMark(txbCwkMark.Text) Dim examMark As Integer = GetMark(txbExamMark.Text) Dim overallMark As Integer

If ( cwkWeighting >= 0 ) AND ( cwkMark >= 0 ) AND ( examMark >= 0 ) Then overallMark = GetOverallMark( cwkWeighting, cwkMark, examMark ) lblOverallMark.Text = CStr( overallMark ) lblResult.Text = GetResult( overallMark ) Else If cwkWeighting < 0 Then HandleInvalidInput(txbCwkWeighting) End If If cwkMark < 0 Then HandleInvalidInput(txbCwkMark) End If If examMark < 0 Then HandleInvalidInput(txbExamMark) End If End IfEnd Sub

Get user inputs

Do Calculation

Output results

Check inputs

Handle invalid inputs

Page 37: Lecture (8)Decision Making

37

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example - module marks (5)

Private Function GetMark(ByVal str As String) As Integer ' validate input string If ( IsNumeric(str) ) Then If (CInt(str) >= 0) And (CInt(str) <= 100) Then Return CInt(str) ' a number between 0 and 100 Else Return -1 ' to notify user about negative input End If Else Return -2 ' to notify user about non-numeric input End IfEnd Function

Private Function GetOverallMark(ByVal cwkWeighting As Integer, _ ByVal cwkMark As Integer, ByVal examMark As Integer) As Integer Dim overallMark As Double overallMark = ( cwkMark * cwkWeighting + _ examMark * (100 - cwkWeighting) ) / 100 ) Return Math.Round( overallMark )End Function

Note: The built-in function IsNumeric(str) returns true if the string str contains a number

Function GetMark

Function GetOverallMark

Note: The Math function Round(x) returns the number x rounded to the nearest integer

Page 38: Lecture (8)Decision Making

38

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example - module marks (4)

Private Sub HandleInvalidInput(ByVal txtBox As TextBox) Dim msg As String

Select Case txtBox.Name Case "txbCwkWeighting" msg = "Coursework weighting is between 0 and 100." Case "txbCwkMark" msg = "Coursework mark is between 0 and 100." Case "txbExamMark" msg = "Exam mark is between 0 and 100. " Case Else msg = txtbox.Name & " is an unknown. " End Select MessageBox.Show(msg) ' show error message txtBox.Text = " " ' clear input box txtBox.Focus() ' set focus on the input boxEnd Sub

Select

Subroutine HandleInvalidInput

Page 39: Lecture (8)Decision Making

39

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example - module marks (6)

‘ get the final result for a given overall markPrivate Function GetResult(ByVal mark As Integer) As String

Select Case mark Case 70 To 100 Return "Distinction" Case 60 To 69 Return "Merit" Case 40 To 59 Return "Pass" Case 0 To 40 Return "Fail" Case Else Return CStr(mark) & " is not a valid mark." End Select

End Function

Function method GetResult

Select

Page 40: Lecture (8)Decision Making

40

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Example - module marks (7)

Private Sub cmdClear_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdClear.Click

txbCwkWeighting.Text = "" txbCwkMark.Text = "" txbExamMark.Text = "" lblResult.Text = ""End Sub

Private Sub cmdClose_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdClose.Click

Close()End Sub

Page 41: Lecture (8)Decision Making

41

UN

IVE

RSIT

I K

UA

LA L

UM

PU

RM

ala

ysia

Fra

nce I

nsti

tute

FSB23103

Summary

• Program control to specify the order in which program codes are

executed• Flow diagram

a schematic representation of an algorithm • Control structures

sequence selection repetition (to be covered in next lecture)

• Selection structures If If … Else Nested IF – If … ElseIf Select