10
© 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem- Solving Approach Decisions Nested on True Branch If condition1 Then Statements for steps ABC If condition2 Then statement(s) for condition1 and condition2 true Else statement(s) for condition1 true and condition2 false End If Statements for steps XYZ Else statement(s) for condition1 false End If Condition1 ? False True Steps to process if condition 1 is false True Condition2 ? False Steps to process if condition1 is true and condition2 is false Steps to process if both condition1 and condition2 are true Steps ABC Steps XYZ

Decisions Nested on True Branch

  • Upload
    faunus

  • View
    25

  • Download
    0

Embed Size (px)

DESCRIPTION

Decisions Nested on True Branch. False. True. Condition1 ?. Steps ABC. Steps to process if condition 1 is false. False. True. Condition2 ?. Steps to process if condition1 is true and condition2 is false. Steps to process if both condition1 and condition2 are true. Steps XYZ. - PowerPoint PPT Presentation

Citation preview

Page 1: Decisions Nested on True Branch

© 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Decisions Nested on True BranchIf condition1 Then

Statements for steps ABC

If condition2 Then

statement(s) for

condition1 and condition2 true

Else

statement(s) for

condition1 true and condition2 false

End If

Statements for steps XYZ

Else

statement(s) for condition1 false

End If

Condition1 ?False True

Steps to process if condition 1 is false

TrueCondition2 ?False

Steps to process if condition1 is true and condition2 is false

Steps to process if both condition1 and condition2 are true

Steps ABC

Steps XYZ

Page 2: Decisions Nested on True Branch

© 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Decisions Nested on False BranchIf condition1 Then

statement(s) for condition1 true

Else

Statements for steps ABC

If condition2 Then

statement(s) for

condition1 false and condition2

true

Else

statement(s) for

condition1 and condition2 false

End If

Statements for steps XYZ

End If

Condition1 ?TrueFalse

Steps to processif condition1 istrue

Condition2 ?TrueFalse

Steps to processif condition1 is false and condition2 is true

Steps to processif both condition1 and condition2 are false

Steps ABC

Steps XYZ

Page 3: Decisions Nested on True Branch

© 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Nested Decisions on False (ElseIf)

If condition1 Then

statement(s) for condition1 true

ElseIf condition2 Then

statement(s) for condition1 false and condition2 true

Else

statement(s) for both condition1 and condition2 false

End If

Steps to processif condition1 is false and condition2 is true

Condition1 ?TrueFalse

Steps to processif condition1 istrue

Condition2 ?TrueFalse

Steps to process if both condition1 and condition2 are false

False branch contains a complete decision structure and no intervening steps before and after the decision

Page 4: Decisions Nested on True Branch

© 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Select Case: Special Case of Nested Decisions on False (ElseIf)

Select Case expression

Case cond1

statement(s) for cond1 true

Case cond2

statement(s) forcond1 false andcond2 true

:

Case condn

statement(s) forcond1-condn-1

falseand condn true

Case Else

statement(s) foral conditions

false

End Select

When every decision in nested if compares values against the same expression

cond1 True

False

Steps to process if cond1 True

Case expression

cond2 True

False

Steps to process if cond2 True

condn TrueSteps to process if condn True

Steps to process if condn False

Page 5: Decisions Nested on True Branch

© 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Limitations of Data Validation It can become quite difficult to handle every

type of problem during data validation that could arise.

VB provides the option of including error handlers to augment or as an alternative to data validation processing. Supplying too large of a value to a conversion

function will trigger an overflow error. During division operations, a zero divisor will

trigger a division by zero run time error. Passing the wrong type of data to a function or

procedure will trigger a type mismatch error.

Page 6: Decisions Nested on True Branch

© 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

VB’s Built-in Error Handling What happens when errors occur in VB?

Number, source and description of VB’s Err object are updated

VB displays error message with above information Program breaks or terminates

Page 7: Decisions Nested on True Branch

© 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Writing Error Handlers Instruct VB to bypass its error handling

On Error Goto PgmLabelName Bypass error handler if no errors triggered

Add Exit Sub after last line of procedure body and right before End Sub

Add Error Handler code between Exit Sub & End Sub Add Error Handler label

PgmLabelName:

Add Error Handler code

Page 8: Decisions Nested on True Branch

© 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Error Handler Example 1 Similar to VB’s built-in error handling Important difference is that the program does not

terminate execution

Private Sub cmdCalc_Click()

On Error GoTo HandleErrors ‘ override VB’s intrinsic error handling

If DataVal Then

‘ do normal processing

End If

Exit Sub ‘ all done if it was the normal case

HandleErrors: ‘ starting point of error handler

Call MsgBox(Err.Number & “: “ & Err.Description, _

vbOkOnly+vbInformation)

End Sub

Page 9: Decisions Nested on True Branch

© 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Error Handler Example 2Private Sub cmdCalc_Click()

On Error GoTo HandleErrors

If DataVal Then

‘ do normal processing

End If

Exit Sub

HandleErrors:

If Err.Number = 6 Then

Call MsgBox(“Overflow occurred in formula”, vbOkOnly+vbInformation)

ElseIf Err.Number = 11 Then

Call MsgBox(“Division by 0 not allowed”, vbOkOnly+vbInformation)

ElseIf Err.Number = 13 Then

Call MsgBox(“Check data type compatibility”, vbOkOnly+vbInformation)

Else

Call MsgBox(Err.Number & “: “ & Err.Description, vbOkOnly+vbInformation)

End If

End Sub

Page 10: Decisions Nested on True Branch

© 1999, by Que Education and Training, Chapter 6, pages 289-312 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach

Error Handler Example 3Private Sub cmdCalc_Click()

On Error GoTo HandleErrors

If DataVal Then

‘ do normal processing

End If

Exit Sub

HandleErrors:

Select Case Err.Number

Case 6 Then

Call MsgBox(“Overflow occurred in formula”, vbOkOnly+vbInformation)

Case 11

Call MsgBox(“Division by 0 not allowed”, vbOkOnly+vbInformation)

Case 13

Call MsgBox(“Check data type compatibility”, vbOkOnly+vbInformation)

Case Else

Call MsgBox(Err.Number & “: “ & Err.Description, vbOkOnly+vbInformation)

End Select

End Sub