Computermade Decisions - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Computermade Decisions

Description:

If the light is red, stop. If it is Labor Day, don't come to ... Display Abort, Retry, and Ignore buttons. 2. vbAbortRetryIgnore. Display OK and Cancel buttons ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 28
Provided by: benma9
Category:

less

Transcript and Presenter's Notes

Title: Computermade Decisions


1
Computer-made Decisions
Chapter 3 4
2
Overview
  • Variable Scope
  • Conditionals
  • Relational Operators
  • AND, OR, NOT
  • If Statements
  • MsgBox function

3
Conditionals
  • Sometimes it is necessary to alter ones
    behavior, depending on existing conditions
  • Examples
  • If the light is red, stop
  • If it is Labor Day, dont come to class,
    otherwise attend lecture and lab
  • If there is a quiz on Monday, then read the book
    Sunday

4
Conditionals
  • True or False conditions are created
  • when comparing variables with relational
    operators
  • by evaluating a boolean variable
  • Relational Operators
  • gt Greater than lt
    Less than
  • Equal to ltgt
    Not equal to
  • gt Greater than or equal to lt Less
    than or equal to

5
Compound Conditions
  • You may join separate conditions with the AND
    and the OR to create compound conditions
  • The AND is data reducing it reduces choices that
    fit
  • The OR is data expanding it increases the
    choices that fit

Blue Cars AND Four Doors is
????? Blue Cars OR Four Doors is
?????
Blue Cars
A B C
Four Doors
6
Compound Conditions
Union Both
Alternation Either
And
F
Or
T
T
F
T
T
T
T
F
T
F
F
F
F
T
F
7
Compound Conditions
  • Evaluate Negations (NOTs) first
  • Evaluate ANDs left to right
  • Evaluate ORs after all ANDs left to right
  • Parentheses can, as usual override this precedence

8
The If Test
  • A conditional statement that permits portions of
    code to be executed only if and when some special
    condition applies
  • I'd like root beer if they have it, otherwise
    I'll have a cola
  • If it is raining, bring an umbrella

9
If Test
  • If (Condition) Then
  • ' (do true stuff)
  • Else
  • ' (do false stuff)
  • End If

10
If Test (short form)
  • If (Condition) Then
  • ' (do true stuff)
  • End If
  • use only if there is nothing to do in the Else
    branch

11
IF Test Example
  • Private Sub cmdDo_Click()
  • If cmdDo.Caption "Talk" Then
  • lblBox.Caption "ding ding"
  • cmdDo.Caption "clear"
  • Else 'in this case, cmdDo "clear"
  • lblBox.Caption ""
  • cmdDo.Caption "Talk"
  • End If
  • End Sub

12
IF Test Example
ltlt alternate on clicking gtgt
13
IF Test Example
  • Private Sub cmdBlink_Click()
  • If lblBox.Visible True then
  • lblBox.Visible False
  • Else
  • lblBox.Visible True
  • End If
  • End Sub
  • Since lblBox.Visible is boolean why not use
  • If lblBox.Visible then

14
ElseIf Clause
  • If condition1 Then
  • statements to process when condition1 is true
  • ElseIf condition2 Then
  • statements to process when condition1 is false
    and condition2 is true
  • Else
  • statements to process when condition1 is false
    and condition2 is false
  • End If 'ElseIf is much like select-case

15
Nested Selection Structure
  • A nested selection structure is one in which
    either the true path or the false path includes
    yet another selection structure
  • Any of the statements within either the true or
    false path of one selection structure may be
    another selection structure

16
Nested If Example
  • Write an if statement that assigns a sales tax
    rate to the sngTax variable
  • The tax rate is determined by the state code
    stored in the intCode variable
  • Codes of 1 and 3 represent a 4 rate
  • A code of 2 represents a 5 rate
  • All other codes represent a 2 rate

17
Nested If Example
  • If intCode 1 Or intCode 3 Then
  • sngTax .04
  • Else
  • If intCode 2 Then
  • sngTax .05
  • Else
  • sngTax .02
  • End If
  • End If

18
Nested If Example
  • Write a selection structure that assigns a bonus
    to the sngBonus variable
  • The bonus is determined by the salespersons code
    (intCode) and, in some cases, by the sales amount
    (sngSales)
  • If the code is 1
  • and the salesperson sold at least 10,000, then
    the bonus is 500
  • otherwise these salespeople receive 200
  • If the code is 2
  • and the salesperson sold at least 20,000, then
    the bonus is 600
  • otherwise these salespeople receive 550
  • All others receive 150

19
Nested If Example
  • If intCode 1 Then
  • If sngSales gt 10000 Then
  • sngBonus 500
  • Else
  • sngBonus 200
  • End If
  • ElseIf intCode 2 Then
  • If sngSales gt 20000 Then
  • sngBonus 600
  • Else
  • sngBonus 550
  • End If
  • Else
  • sngBonus 150
  • End If

20
Nested If Example
  • If intCode 1 And sngSales gt 10000 Then
  • sngBonus 500
  • Else
  • If intCode 1 And sngSales lt 10000 Then
  • sngBonus 200
  • Else
  • If intCode 2 And sngSales gt 20000 Then
  • sngBonus 600
  • Else
  • If intCode 2 And sngSales lt 20000 Then
  • sngBonus 550
  • Else
  • sngBonus 150
  • End If
  • End If
  • End If
  • End If

21
ElseIf Example
  • If intCode 1 And sngSales gt 10000 Then
  • sngBonus 500
  • ElseIf intCode 1 And sngSales lt 10000 Then
  • sngBonus 200
  • ElseIf intCode 2 And sngSales gt 20000 Then
  • sngBonus 600
  • ElseIf intCode 2 And sngSales lt 20000 Then
  • sngBonus 550
  • Else
  • sngBonus 150
  • End If

22
The Message Box
  • VB allows the user to generate message boxes as a
    part of a user program. When invoked they look
    something like this

23
Format of MsgBox
  • A simple example of the MsgBox function
  • Dim intX As Integer
  • intX MsgBox("Read the Password")

24
The Details
  • Dim intX As Integer
  • intX MsgBox("Do this now", _
  • vbCritical, "Prompting Issue")

25
Response Choices
Dim intZ As Integer intZ MsgBox(Should I Do
This, _ vbYesNo ,Prompting Issue)
  • The value of intZ above depends on which
    response is chosen.
  • Yes - intZ 6
  • No - intZ 7
  • (the numerical values are defined by Visual
    Basic.)

26
Icon Choices
  • vbExclamation
  • vbQuestion
  • vbCritical

27
Possible values for the 2nd argument
Write a Comment
User Comments (0)
About PowerShow.com