VB Decisions - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

VB Decisions

Description:

VB Decisions & Conditions – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 34
Provided by: admi2691
Category:

less

Transcript and Presenter's Notes

Title: VB Decisions


1
VB Decisions Conditions
2
Covered Tipics
  • Conditions defined
  • Relational operators
  • Logical Operators
  • If Blocks
  • Read Create Flowcharts (Logic)

3
A Condition
  • Definition
  • Condition is an expression involving relational
    operators (such as gt or ) or logical operators
    (and or) that is either true or false.
  • Example
  • Frog Frog
  • 12 gt 8


4
Relational Conditions
  • 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
Examples of Conditions
  • txtName.text ltgt "Frockmeister"
  • Val(txtAge.Text) gt 25
  • optPrintForm.Value True
  • chkDiscardStyles.Value False
  • (Val(txtInterestRate.Text) / 12) lt 0.05

6
Mathematical Relational Operators
7
Is the Condition True
  • 1 lt 1
  • 1 lt 1
  • car lt cat
  • Dog gt dog
  • Remember the ASCII Table

8
Are These Conditions True?
  • Let a 4 and b 3
  • Let c hello and d bye
  • (a b) lt (2 a)
  • (Len(c) b) (a / 2)
  • C lt (good d)
  • The function Len() returns a numeric value that
    indicated the number of characters in the string,
    inside the function.

9
Logical Operators
  • The three main logical operators are
  • And
  • Or
  • Not
  • Some programming conditions require more
    complicated comparison then just true or false

10
Logical Operators allow us to..
  • Evaluate condition1 and condition2 based on the
    following
  • condition1 and condition2
  • condition1 or condition2
  • condition1 not condition2

11
With Relational and Logical Conditions We Can
  • Comparing Numeric Variables Constants
  • Comparing Strings
  • Comparing text property of text boxes
  • Uppercase and lowercase character comparisons
  • Compound conditions

12
Comparing Numbers Strings
  • If txtPrice.Text
  • curRetailPrice Then ...
  • If Val(txtPrice.Text) curRetailPrice Then ...
  • Compare these two slightly different conditions
  • If txtLastName.Text Smith
  • If Lcase(txtLastName.Text) smith

13
Are these conditions T/F?
  • Let the string variable answ Y
  • (answ Y) or (answ y)
  • (answ Y) and (answ y)
  • Not (answ y)

14
Using Parentheses
  • Using parentheses can improve readability.
  • Place parentheses in this equation to help
    evaluate it.
  • a lt b c Or d lt e And Not f g

15
Answer
  • a lt (b c) Or (d lt c) And (Not f g)
  • A condition involving numeric variables is
    different from an algebraic truth.
  • The condition (a b) lt 2 a is not a valid
    algebraic truth because it is not true for all
    values of a and b
  • VB will evaluate it as true if it is correct for
    the current values of the variables

16
Compound Conditions
  • If a1 lt a2 or a3 gt a4 and a5 lt a6 Then
  • And has precedence over Or
  • All comparison operators have precedence over all
    logical operators
  • Use parentheses () to alter the order of
    evaluation

17
Parentheses
  • Parentheses alter the order of evaluation, are
    good for explicitly documenting groupings, and
    are good if you are unsure of precedence.
  • If (curValue gt 100) and (curValue lt 200) Then
  • lblMessage.Caption "Value
  • between 101 and 199"
  • End If

18
IF Statement Structure (1)

19
IF Statements Structure (2)
If School Day
False
True
Stay In Bed
Go to Class
Action
Action
20
If Examples in VB
  • If Then (all on one line)
  • If strName "Jamie" Then lblMessage.Caption _
  • "Hello Jamie"
  • Block If Then End If
  • If strName "Sam" Then
  • lblMessage.Caption "Hello Sam"
    another statement
  • End If

Time
21
Block IfThenElseEnd If
  • If strName "Connie" Then
  • lblMessage.Caption "Hello Connie" another
    statement
  • Else
  • lblMessage.Caption "Hello Stranger"
    another statement
  • End If

22
IfthenElseIfthenend if
If School Day
True
False
If Sunny
Stay In Bed
False
True
Play Tennis
Go to Class
23
Example of Nested IF
If costs revenue Then picResult.Print Break
even Else If costs lt revenue Then profit
revenue costs picResult.Print Profit is
_ FormatCurrency(profit) Else
loss costs revenue picResult.Print Loss is
_ FormatCurrency(loss) End
if End if
24
If Block and Logic Operators
  • The If Block can be combined with logic operators
    to compare more than one condition
  • If (answer gt .5) and (answer lt 1) Then
  • picSolution.Print Good
  • Else
  • picSolution.Print No
  • End if

25
Additional Information
  • It is not necessary to indent If Blocks
  • We indent to improve readability
  • Some times the use of relational operators make
    the If Block structure easier to understand
  • If condition1 Then
  • If condition2 Then
  • action
  • End if
  • End if
  • If condition1 and condition2 then
  • action
  • End if

26
The Select Case Block
  • Executes a statement based on the value of a
    variable
  • Select Case bytMonth
  • Case 1, 3, 5, 7, 8, 10, 12
  • number_of_days 31
  • Case 2
  • number_of_days 28
  • Case 4, 6, 9, 11
  • number_of_days 30
  • End Select

27
Looking for a Number
  • Select Case Score Case is 0
  • ' Score is still 0
  • lblMsgbox ("Keep trying!") Case 1To 4
  • ' Score is between 1 and 4.
  • lblMsgbox ("Making a good start!") Case 5,
    6,7
  • ' Score is either 5, 6, or 7.
  • lblMsgbox ("Nearly there!") Case Is gt 7
    And Number lt 11
  • ' Score is 8 ,9, or 10
  • lblMsgbox ("Getting close! ") Case Else
  • ' Score is one of the other values.
  • lblMsgbox (Number is to large! ") End
    Select

28
The Problem
  • In New Jersey the state income tax is based on a
    persons base income at three different interval
    levels
  • The tax on Income lt 20,000
  • is taxed at 2 of income
  • The tax on Income gt 20,000 but lt 50,000
  • is 400 plus 2.5 of (income -20,000)
  • The tax on income gt 50,000
  • is 1150 plus 3.5 of (income - 50,000)

29
(No Transcript)
30
For Next Block
  • For index lower limit to upper limit
  • Coding statements
  • This could be code to print the records in a
    file.
  • Print one record from a file
  • May contain condition blocks
  • Next index

31
Action of For...Next structure
Counterltending value
Countergtending value
For counter start To end statement1 statement2
. . .
For counter start To end statement1 statement2
. . .
Increment counter
Next counter
Next counter statement
The Index can also be called the counter.
32
Iterations of a For...Next loop
  • Private Sub cmdForNextTest_Click()
  • Dim X As Integer define the index
  • For X 1 To 3 1 lower limit
  • picDisplay.Print X, 3 upper limit
  • Next X
  • End Sub

What will be the value of X when the loop
terminates? What will be inside the picture box?
33
Problem Check
  • Any Questions about the homework problem?
Write a Comment
User Comments (0)
About PowerShow.com