Visual Basic VB: Decisions and Iteration PowerPoint PPT Presentation

presentation player overlay
1 / 21
About This Presentation
Transcript and Presenter's Notes

Title: Visual Basic VB: Decisions and Iteration


1
Visual Basic (VB)Decisions and Iteration
2
Decision Making
  • Remember we talked about an objects state.
  • Depending on state, which may well be determined
    by a variables value inside an object, we often
    need to take different courses of action.
  • Remember the hungry/not hungry scenario?
  • To make such a decision, we need to carry out a
    test to see if some condition is true or false,
    e.g.
  • To withdraw money from the bank, you need to
    check the account balance.
  • If the balance is greater than the amount that
    you want to withdraw, then you withdraw it
    otherwise you can arrange an overdraft.

3
Decision Making in VB
  • Two-way selection
  • If test Then
  • ElseIf test Then
  • ElseIf test Then
  • Else
  • End If
  • Multi-way selection
  • Select Case testExpression
  • Case expressionList
  • Case expressionList
  • Case Else
  • End Select

4
The If Statement
  • Test is carried out and is either true or false.
  • If true, then the statements associated with the
    Then are executed
  • Otherwise, if there is an Else statement, then
    the statements associated with the Else are
    executed.
  • If we wanted to set a student's grade depending
    on the exam mark if mark is greater than or
    equal to fifty, the grade will be pass otherwise
    the grade will be fail.
  • If mark gt 50 Then
  • grade "pass
  • Else
  • grade "fail
  • End If

5
The Select Case Statement
  • The testExpression is evaluated once and compared
    with each expression in the expressionList
    associated with each Case.
  • If there is a match, then the statements
    associated with that Case are executed.
  • If a customer wants to buy a product and supply
    the product number and quantity, the computer
    calculates the total price
  • Select Case productNo
  • Case Is 21
  • unitPrice 24.5
  • Cost unitPrice quantity
  • Case 54, Is gt 99, 78 To 83, ProductOfTheWeek
  • unitPrice 14.3
  • Cost unitPrice quantity
  • Case Else
  • unitPrice 10.0
  • Cost unitPrice quantity
  • End Select

Is there a better way to write this?
6
Iteration
  • Enables us to repeat a set of instructions many
    times.
  • In VB there are various types of iteration
    statement
  • Do While test
  • statement
  • ...
  • Loop
  • ---------------------------
  • Do
  • statement
  • ...
  • Loop While test
  • ---------------------------
  • For counterstartValue To endValue Step increment
  • statement
  • ...
  • Next

Here we can replace While with Until and reverse
to logic
Theres also a For Each...Next, which you can
explore yourself
7
Iteration
  • The statements are known as the loop body.
  • These are executed repeatedly while the condition
    is true.
  • When the test for staying in the loop becomes
    false, the program moves to the statement after
    the loop.
  • The Do While and the For loops perform the test
    before the loop body is executed.
  • If the test evaluates to false the very first
    time it is tested then the loop body is never
    executed.
  • In contrast, the Do...Loop statement tests the
    condition after the loop body has been executed
  • the loop body is executed at least once.

8
Which one to use?
  • Well weve just heard about the difference of the
    Do loop over the other two
  • The loop body is executed once in a Do loop.
  • If were waiting for a state to change then Do
    While and Do loops are useful.
  • If were moving though an array then the For loop
    is neater than a Do While
  • But whats an array?

9
Arrays
  • Every item in an array contains some data.
  • You could also think of arrays as tables in rows
    and columns
  • The individual entries, or elements, are accessed
    by the array name, followed by the position
    (index), of the entry, eg myData(4).
  • Like a street name and house number provides an
    address.
  • However, array indexes start at 0, so
  • Dim myArray(6) As Integer
  • reserves space for 7 elements, indexed thus 0,
    1, 2, 3, 4, 5, 6.
  • This is a bit different to most other languages.

10
Declaring Arrays
  • As with other variables, arrays must be declared
    before they are used
  • Why?
  • Examples
  • Dim marks(6) As Integer 'creates an array which
    will contain ___ integers
  • Dim sizes(4) As Double 'creates an array which
    will contain ___ doubles
  • Dim names(3) As String 'creates an array which
    will contain ___ strings

11
Setting/Getting Data in Arrays
  • marks(3) 76 'sets the 4th element of the array
    marks to 76
  • sizes(4) 2.5 'sets the 5th element of the
    array, sizes, to 2.5
  • names(0) "Bridget" 'sets the 1st element of
    the array
  • Strudents(0).lastName lastName(1) One array
    sets another
  • Label2.Text names(3) get the 4th element of
    an array
  • TextBox2.Text myControls(i).text gets the
    ith element of array object

12
Returning to Iterating over Arrays
  • Private Sub blahblah()
  • Dim numbers(Size) As Integer
  • Dim i As Integer
  • numbers(0) 0
  • For i 1 To Size Step 1
  • numbers(i) i numbers(i - 1)
  • Debug.WriteLine(Str(numbers(i)))
  • Next
  • End Sub
  • Private Sub moreblah()
  • Dim numbers(Size) As Integer
  • Dim i As Integer
  • numbers(0) 0
  • i 1
  • Do While i lt Size
  • numbers(i) i numbers(i - 1)
  • Debug.WriteLine(Str(numbers(i)))
  • i i 1
  • Loop
  • End Sub

Which is better and why?
13
Anatomy of a Loop
  • Looking at the previous slide you can see three
    components common to all loops
  • Initialisation
  • Test
  • Increment

14
Multi-Dimensional Arrays
  • Weve already said that arrays could be thought
    of as tables, but those we already seen would
    only have one row, ie one dimension.
  • Imagine we could have more than one dimension

15
Multi-Dimensional Arrays
  • Declaring
  • Dim printedTable(1000,5) As String
  • One thousand one rows and six columns
  • Space for 6006 strings.
  • Setting
  • printedTable(0,0) First Name
  • printedTable(0,1) Last Name
  • Getting
  • headerRowLabel(0).Text printedTable(0,1)
  • headerRowLabel(1).Text printedTable(0,2)

16
Dynamic Arrays
  • Sometimes you dont know how big the array will
    be when you initially declare it
  • When you read in a file for instance.
  • Dont want to declare a huge array initially
  • Might run out of memory
  • We need to re-size arrays when necessary using
    ReDim
  • Dim charactersInString() declare with empty
    dimension list
  • stringLength stringOfInterest.Length
  • ReDim charactersInString(stringLength-1)
  • ReDim destroys the contents of the array unless
    you use
  • ReDim Preserve .

17
Array Lists
  • A special kind of dynamic array built into some
    of the standard controls.
  • Has really handy methods to add, delete, count,
    etc. items from the array
  • Dim listOfMates As ArrayList
  • Dim numberOfMates As Integer
  • listOfMates.Add("bob")
  • listOfMates.Add(tam")
  • numberOfMates listOfMates.Count
  • listOfMates.RemoveAt(0)

18
Array Lists in List Boxes
Imagine we had
19
Inside the List Box
  • we have a special secret array list called
    Items
  • We can get at it by saying

20
We need some subs
  • After an enter name button click
  • If TextBox1.Text.Length gt 0 Then
  • ListBox1.Items.Add(TextBox1.Text)
  • End If
  • For a count,
  • MessageBox.Show(CStr(ListBox1.Items.Count))
  • For a remove,
  • If ListBox1.SelectedIndex gt 0 Then
  • ListBox1.Items.RemoveAt(ListBox1.Selec
    tedIndex)
  • End If
  • For a search, you need all sorts that you might
    like to figure out for yourself

21
Next Time
  • well discuss procedures.
  • In the meantime, you should be near finishing all
    the lab exercises.
  • You should be on the verge of starting your
    coursework by now?!
  • Remember, you should be working outside of
    timetabled classes.
  • Also remember, developing away from the labs can
    be problematic when we assess your work.
  • You should try the exercises given on the first
    tutorial sheet available from the module Web
    pages.
Write a Comment
User Comments (0)
About PowerShow.com